0

If I ask Twig to render example.twig it will generate a cache file named something like 8e8456gffc99c672976eff.php (if cache is enabled).

Is there a way to then take 8e8456gffc99c672976eff.php, and make Twig run it directly without even having access to the original example.twig file?

I want something like $this->twig->renderCache('8e8456gffc99c672976eff.php', $data); but haven't found a straightforward way to do it (I can hardcode it but would prefer a generalized solution).

Edit: Current solution (for 1.x at least) is to init Twig, include_once the php cache file, call render and in Twig_Environment::loadTemplate simply overwrite the template class name. The barely generalized solution is to do $cls = '__TwigTemplate_' . str_replace('.twig', '', $name); so that I don't have to hardcode the name in there, only change the class name in the cache file.

However, I'd prefer a solution that doesn't require any changes to Twig itself.

3
  • 2
    My gut reaction is this feels like an XY problem. Can you explain why you'd want to do this? Commented Sep 4, 2024 at 21:49
  • @ChrisHaas I don't want to distribute the original twig file but I don't care enough to properly encrypt/obfuscate/etc. the php cache file. Commented Sep 4, 2024 at 22:29
  • Well, you can manually compile a twig file, which would give you a string that you could place anywhere. Otherwise you kind of have a chicken and egg situation, where your application compiles twig to PHP, but if that PHP exists don’t compile the twig, just include it. My gut says twig was not meant for this, so you’ll need to hack through a bit, or have two apps, where the second is the deliverable which only supports the compiled files. Commented Sep 5, 2024 at 2:28

0

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.