Twig's dump function returns a blank screen

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"

Tags:

Twig

Symfony