Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

symfony - Twig's dump function returns a blank screen

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

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.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.

What exactly do you need to do

  • 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)

OR

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: Question's asked in 2013, but I hope it helps because I had this problem now.

My context:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...