From: Fabien Potencier Date: Fri, 13 Jul 2012 06:40:06 +0000 (+0200) Subject: fixed the template escaper guesser example X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=0d5a63993726057d28d68884112cc48508ab835e;p=web%2Fkonrad%2Ftwig.git fixed the template escaper guesser example --- diff --git a/doc/recipes.rst b/doc/recipes.rst index 59a6dfb..5f98504 100644 --- a/doc/recipes.rst +++ b/doc/recipes.rst @@ -335,25 +335,31 @@ you have some dynamic JavaScript files thanks to the ``autoescape`` tag: But if you have many HTML and JS files, and if your template names follow some conventions, you can instead determine the default escaping strategy to use based on the template name. Let's say that your template names always ends -with ``.html`` for HTML files and ``.js`` for JavaScript ones, here is how you -can configure Twig:: +with ``.html`` for HTML files, ``.js`` for JavaScript ones, and ``.css`` for +stylesheets, here is how you can configure Twig:: - function twig_escaping_guesser($filename) + class TwigEscapingGuesser { - // get the format - $format = substr($filename, strrpos($filename, '.') + 1); - - switch ($format) { - 'js': - return 'js'; - default: - return 'html'; + function guess($filename) + { + // get the format + $format = substr($filename, strrpos($filename, '.') + 1); + + switch ($format) { + case 'js': + return 'js'; + case 'css': + return 'css'; + case 'html': + default: + return 'html'; + } } } $loader = new Twig_Loader_Filesystem('/path/to/templates'); $twig = new Twig_Environment($loader, array( - 'autoescape' => 'twig_escaping_guesser', + 'autoescape' => array(new TwigEscapingGuesser(), 'guess'), )); This dynamic strategy does not incur any overhead at runtime as auto-escaping