Twig 的转储函数返回一个空白屏幕 [英] Twig's dump function returns a blank screen

查看:31
本文介绍了Twig 的转储函数返回一个空白屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Symfony2 中使用 Twig 的 dump 函数.我已经根据 其说明配置了 Symfony.

I am using Twig's dump function in Symfony2. I have configured Symfony according to its instructions.

我有一个 page 变量和一个 orders 数组.dump 适用于页面,但不适用于订单.当我按订单调用它时,我得到一个白屏 - 没有 php 错误或任何东西.我不知道如何调试这个.

I have a page variable, and an orders array. dump works on page, but not orders. When I call it on orders, I get a white screen - no php errors or anything. I have no idea how to debug this.

有什么想法吗?

推荐答案

一点说明

在这种情况下,白色(空白)屏幕表示 PHP 致命错误:允许的内存大小已耗尽.在我的调查中,我发现 twig 在我安装了 VarDumper 组件时使用了var_dump 函数.

A little explanation

A white (blank) screen in this case means the PHP fatal error: Allowed memory size exhausted. During my investigation, I found that twig uses thevar_dump function while I have VarDumper component installed.

我认为它可以在未安装 VarDumper 组件的情况下工作,但是 symfony 的 VarDumper 组件文档中涵盖的 twig 的 dump() 函数就像一个复杂的解决方案,这很奇怪.

I think its made to work along in case the VarDumper component is not installed, but twig's dump() function covered in symfony's VarDumper component documentation like a complex solution, that's strange.

因此,使用 VarDumper 的 dump() 函数而不是本机 var_dump() 解决了内存问题(因为 VarDumper 将结果转储收集限制为适量).此外,VarDumper 的 dump() 提供了更方便的结果 - 您可以单击树叶来显示/隐藏其内容.

So, using VarDumper's dump() function instead of native var_dump() solves the memory problem (because VarDumper limits result dump collection to adequate amount). Also VarDumper's dump() give more convenient results - you can click on tree leafs to show/hide its content.

  • 安装 VarDumper 组件(如果未安装)
  • 转到文件:vendor/twig/twig/lib/Twig/Extension/Debug.php
  • 查找twig_var_dump 函数
  • 将所有 var_dump() 调用替换为 dump()
  • 删除/注释 ob_start() + ob_get_clean() 构造(需要如果您使用 var_dump() 因为它立即回显数据,但是 dump()更聪明)
  • Install VarDumper component if not installed
  • Go to file: vendor/twig/twig/lib/Twig/Extension/Debug.php
  • Find twig_var_dump function
  • Replace all var_dump() calls to dump()
  • Delete/comment ob_start() + ob_get_clean() construction (which is needed if you use var_dump() as it echoes data immideately, but dump() acting more intelligent)

使用这个复制+替换整个函数:

copy + replace the entire function using this:

function twig_var_dump(Twig_Environment $env, $context)
{
    if (!$env->isDebug()) {
        return;
    }

    $count = func_num_args();
    if (2 === $count) {
        $vars = array();
        foreach ($context as $key => $value) {
            if (!$value instanceof Twig_Template) {
                $vars[$key] = $value;
            }
        }

        dump($vars);
    } else {
        for ($i = 2; $i < $count; $i++) {
            dump(func_get_arg($i));
        }
    }

}

PS:问题是在 2013 年提出的,但我希望它有所帮助,因为我现在遇到了这个问题.

PS: Question's asked in 2013, but I hope it helps because I had this problem now.

我的背景:

"symfony/symfony": "2.5.*"
"symfony/var-dumper": "~2.6"

这篇关于Twig 的转储函数返回一个空白屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆