Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any chance I can avoid using eval in Twig? #2428

Closed
andreasciamanna opened this issue Mar 17, 2017 · 6 comments
Closed

Is there any chance I can avoid using eval in Twig? #2428

andreasciamanna opened this issue Mar 17, 2017 · 6 comments

Comments

@andreasciamanna
Copy link

I've been looking for a way to keep using Twig, while not using eval.

Unfortunately, not all our clients are willing to keep it enabled.

So, rather than googling and asking everywhere else (which I did anyway), I thought that this is probably the best place.

My objective is to allow Twig to work when extensions such as Suhosin are installed and eval is disabled.

I've tried to look at the code in \Twig_Environment::loadTemplate and I can't quite understand when eval('?>'.$content) is called, but it is called several times.

Regardless, I wonder if there is any way I can make Twig stop using the eval function.

@sstok
Copy link

sstok commented Mar 20, 2017

eval is only used when you don't use a Cached template, when you use the cache eval is not used.

@stof
Copy link
Member

stof commented Mar 20, 2017

@sciamannikoo if you enable the filesystem cache, eval will be used only in case of race condition on the cache access (which can be avoided if you take care of parsing all templates of your project before putting your server under load)

@rhukster
Copy link

Would be great if an alternative solution to eval() could be used. This has come up in Grav CMS chat also.

@stof
Copy link
Member

stof commented Apr 13, 2017

When you enable the filesystem cache and you don't have race conditions during the cache warmup process (i.e. you're not running your cache warming under load, or you're lucky when doing it under load), eval won't be called, as the cache file will be loaded through require instead.
And once the cache warming is done, cache files are always loaded through require (allowing to benefit from OPCache for the compiled template code)

@stof
Copy link
Member

stof commented Apr 13, 2017

if you disable the filesystem cache, there is no way to avoid the usage of eval (well, there might be some crazy way based on stream wrappers, but it might be limited by allow_url_include, which is a feature which is even more likely to be disabled for security reasons than eval and so won't help), as the template is compiled to PHP code which must then be loaded by PHP.

@fabpot
Copy link
Contributor

fabpot commented May 22, 2017

Closing as @stof explained how that works really well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants