0

I'm attempting to render out a Twig template that has no layout, just a few variables and some loops.

When I call the render() function on Twig, it outputs a block of PHP code for the following class:

php
/** path/to/my/template.html.twig */
class __TwigTemplate_435244378aba3a3f94258b7d2af4d53eb7a41acb741dd3ad0efcac038b621c67 extends Twig_Template
{
    // bunch of methods for Twig_Template, 
    // including the compiled version of my template
}

After this it gives me a stack trace with the following exception:

Failed to load Twig template "path/to/my/template.html.twig", index "": cache is corrupted in "path/to/my/template.html.twig".

I'm not even using a cache with this app currently, though adding a cache doesn't seem to make a different. I'm using Twig like this:

// Factory to return a new Twig environment
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/../../views/');
return new \Twig_Environment($loader);

// My class has $this->twig set to the above object
$this->twig->render('path/to/my/template.html.twig', [
    'report' => $report, 
    'file' => $file
]);

Twig seems to be able to read in my template, as the block of PHP code it outputs in the error has a properly compiled version of the template. Attempting to set a cache directory that is writable still results in the same error.

I'm using Twig 1.34.4 and PHP 5.6.29 under Apache.

EDIT

A bit of success, in a way. It seems that Twig is never evaling the code that it's generating.

I edited the vendor/twig/twig/lib/Twig/Environment.php file and on line 448 added the following:

eval("?><?" . $content);

After that my template renders just fine. That leads me to the conclusion something else in Twig is breaking, but this isn't a long-term solution since I shouldn't be editing the vendor files.

The block starting at line 456 seems to indicate that $content should have the opening <? but mine doesn't. So that could be screwing with the compilation.

Time for more digging.

1 Answer 1

1

I finally figured it out. It wasn't Twig's fault.

Our deployment process was leaving old files on the disk, so I was running with only part of 1.34.4 upgraded.

Fixed that and everything works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.