twigphp / Twig

Twig, the flexible, fast, and secure template language for PHP
https://twig.symfony.com/
BSD 3-Clause "New" or "Revised" License
8.15k stars 1.25k forks source link

Invalid escaping strategy {% extends "base.html" %} #724

Closed heckur closed 12 years ago

heckur commented 12 years ago

I'm getting the next error in a child template (using 1.8):

Fatal error: Uncaught exception 'Twig_Error_Runtime' with message 'Invalid escaping strategy "{% extends "base.html" %} {% block content %}

I just started a new project copying the basic templates structure from another project that uses version 1.7 and works perfect, the layout at the moment is pretty simple: a parent html that defines 3 blocks, a child that defines one of them... Is there a new requisite i must follow that is not documented yet?

Thanks!

katanacrimson commented 12 years ago

might be a regression introduced with the new embed tag stuff

@heckur can you provide a quick couple of templates which reproduces this?

heckur commented 12 years ago

Sure, i just made these simple ones for a quick test and exception happens on version 1.8:

base.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>

testing.html

{% extends "base.html" %}

{% block content %}
  {{ category }}
{% endblock %}
fabpot commented 12 years ago

Looks like it is related to the new management of the autoescaping strategy. Can you copy/paste the script that configures Twig?

heckur commented 12 years ago

Sure:

require_once dirname(__FILE__).'/lib/Twig/Autoloader.php';
Twig_Autoloader::register();

# Custom function to render templates with Twig
function xrender($template, $vars = array(), $funcs = array())
{
  $loader = new Twig_Loader_Filesystem(dirname(__FILE__).'/views');
  $twig = new Twig_Environment($loader);
  foreach($funcs as $func)
    $twig->addFunction($func, new Twig_Function_Function($func));
  if(!empty($_SESSION['flash']))
    $twig->addGlobal('flash', $_SESSION['flash']);
  if(!empty($_SESSION['user']))
    $twig->addGlobal('user', $_SESSION['user']->display_name);
  return $twig->render($template, $vars);
}
katanacrimson commented 12 years ago

Odd, I just did a checkout of 1.8.0 on my own app and didn't do anything to the templates, and it isn't having any problems (and yes, this is on a clean cache).

https://github.com/damianb/homebooru/blob/master/app/views/status.twig.html All I've got in that file is basically what he depicts above, for structure. There's an extends tag, and a block being defined. The layout file is damn near the same structure as above as well; I'm not showing any issues at all here. I'm using Twig_Loader_Filesystem as well, with a naming structure of {$filename}.twig.html without any issues; autoescape is on and working properly. The only configuration I'm using that's abnormal is "debug" is on.

Could there be something else possibly coming into play that's causing it to break for him, and not myself?

ED: Just disabled twig debug, still working fine. @heckur, just to make sure, are you working with a clean cache (or have you disabled cache)?

heckur commented 12 years ago

It seems like some external configuration is causing this. I had a Silex project using Twig 1.7, updated it to 1.8 and it's still working. The exception occurs in a project using the Limonade PHP framework with its default configuration, and only when variables are passed to the template. I guess i will have to stick to 1.7 in this project until i find out where Limonade may be interfering.

I'm not using cache b the way.

Moqume commented 12 years ago

I'm having the same problems.

What makes it worse, is that currently it appears to be "random" - on one system it works fine, on another it gives me Twig_Error_Runtime: Invalid escaping strategy "" (valid ones: html, js) from a cached object (ie: at twig_escape_filter() in /.../store/cache/views/86/b6/e7c8229d9ae541bc96ed9cd4cf4d.php line 34).

That particular line in the cached object is using a custom function I've registered with Twig. Here's listing that line that works OK:

echo twig_escape_filter($this->env, $this->env->getExtension('translate')->transFilter("Preview"), "html", null, true);

It doesn't seem to matter if I specify 'autoescape' => 'html' to the Twig engine options. Nor does the debug option appear to have any affect on it. Cache is being used at all times, autoreload set at its default.

Edit: Looking at what @heckur reported, I don't think its an issue with the escaper itself, but more the node handling. His invalid escaping method was reported as "{% extends "base.html" %} {% block content %} - the first lines of the template.

fabpot commented 12 years ago

Are you all using Twig within a Symfony2 project?

Moqume commented 12 years ago

I'm not. I'm using a micro-framework that uses the Symfony2 universal class loader along with Twig - same bootstrapping as Symfony2.

heckur commented 12 years ago

No, not using Symfony2, I use Silex or Limonade PHP. Twig 1.7 works perfect on both, but Twig 1.8 is throwing me this fatal error on Limonade PHP with its default configuration which i haven't had the time to analize.

Moqume commented 12 years ago

One more sample, just taken from a client's website (full paths obscured as ...)

<b>Fatal error</b>:  Uncaught exception 'Twig_Error_Runtime' with message 'Invalid escaping strategy "&lt;ask.html.twig&gt;&lt;/ask.html.twig&gt;" (valid ones: html, js) in "ask.html.twig" at line 11.' in /.../vendor/Twig/lib/Twig/Extension/Core.php:810
Stack trace:
#0 /.../store/cache/views/2f/d6/b33b3e899238ad9c5cd8857be2f6.php(26): twig_escape_filter(Object(Twig_Environment), 'Information reg...', '&lt;ask.html.twig&gt;...', NULL, true)
#1 /.../vendor/Twig/lib/Twig/Template.php(265): __TwigTemplate_2fd6b33b3e899238ad9c5cd8857be2f6-&gt;doDisplay(Array, Array)
#2 /.../vendor/Twig/lib/Twig/Template.php(239): Twig_Template-&gt;displayWithErrorHandling(Array, Array)
#3 /.../vendor/Twig/lib/Twig/Template.php(250): Twig_Template-&gt;display(Array)
#4 /.../ in <b>/.../vendor/Twig/lib/Twig/Extension/Core.php</b> on line <b>810</b><br />
fabpot commented 12 years ago

I see why it fails: the escaping strategy is null, and this is not a valid value. But I don't understand how this can be null, especially if it is random. Can you send me the smallest project/script possible that exhibits the problem so that I can reproduce it and investigate further? You can send it to me by email if you want. Thanks.

fabpot commented 12 years ago

Can you also try with Twig master and tell me if the error is the same?

fabpot commented 12 years ago

Just to be sure, have you flushed the template cache after upgrading?

Moqume commented 12 years ago

Yes, I've tried with cache disabled, removed the entire cache directory (rm -rf store/cache).

It actually seems to add some random data instead of 'html' or 'js' as the escaper value - I don't know where it's retrieving this from, there seems no logic to it. But it might actually be something that's been in the code for a while, as you had a type checks for (bool) that have been removed to allow for strings - so any arbitrary data before would have been converted to true unless empty.

I wish I could give you a tiny project/script to help replicate the issue, but the micro-framework is reliant on WordPress. Hopefully @heckur can provide you with one. I will try the master when time permits.

vendethiel commented 12 years ago

Ok ... I was finally able to track down the issue ... In Twig_Extension_Escaper:79, the "getDefaultStrategy" will try to call the "defaultStrategy". However, if, like me, you have a function named "html", it's gonna pass "$this->defaultStrategy, $filename" to this function. I can suggest checking the return value of this function, or maybe adding a note somewhere : "please check you don't have a function named like an escaper ('html' or 'js'), the strategy escaper guessing would pass the filename to the function"

fabpot commented 12 years ago

@Nami-Doc Thanks for your work on investigating the issue. I would not have found it without your help.