Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig's dump function returns a blank screen

Tags:

twig

symfony

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?

like image 648
Sam Selikoff Avatar asked Feb 16 '13 18:02

Sam Selikoff


1 Answers

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"
like image 157
Arkemlar Avatar answered Sep 20 '22 21:09

Arkemlar