Sam Selikoff
Sam Selikoff

Reputation: 12694

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?

Upvotes: 8

Views: 9042

Answers (5)

Arkemlar
Arkemlar

Reputation: 403

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"

Upvotes: 9

yed
yed

Reputation: 1

The Arkemlar answer works fine.

BUT

after that the twig dump() caused a RuntimeException in the functional test with phpunit in my case.

Upvotes: 0

jrmck
jrmck

Reputation: 21

There is probably no single answer to this problem, as a range of things could cause it. As other people have suggested, increasing the memory limit for PHP may help. In my case this did not help, however, I was able to resolve the issue by installing xdebug.

So far, it seems then that a checklist for resolving this issue would look something like this:

  1. Ensure that the latest version of xdebug is installed
  2. If that doesn't resolve it, try increasing PHP memory limit

Upvotes: 0

axasanya
axasanya

Reputation: 41

I had same issue, increasing the memory_limit up to 1Gb didn't help. In my case I want to call the

    {{ dump() }} 

with no parameters (to see all the available vars in the context of the current template. In my case the following helped: https://stackoverflow.com/a/11500141/2166261 , though the memory_limit issue still persists)

Upvotes: 1

Michael Smith
Michael Smith

Reputation: 409

Most likely you are hitting your PHP memory limit. Try increasing it in the php.ini

Upvotes: 0

Related Questions