Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Twig_Error_Syntax : The filter "raw" does not exist #110

Closed
tomasvts opened this issue Oct 2, 2014 · 0 comments
Closed

Twig_Error_Syntax : The filter "raw" does not exist #110

tomasvts opened this issue Oct 2, 2014 · 0 comments

Comments

@tomasvts
Copy link

tomasvts commented Oct 2, 2014

If I change (https://github.com/Pawka/phrozn/blob/master/tests/Phrozn/Processor/templates/tpl1.twig) and make the a_variable to be printed as a raw variable, the test will fail with the following error:

Twig_Error_Syntax : The filter "raw" does not exist in "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
  <head>
    <title>My Webpage</title>
  </head>
  <body>
    <ul id="navigation">
    {% for item in navigation %}
      <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
    {% endfor %}
    </ul>

    <h1>My Webpage</h1>
    {{ a_variable|raw }}
  </body>
</html>
" at line 14
 /Users/tomas/phrmod/vendor/twig/twig/lib/Twig/ExpressionParser.php:575
 /Users/tomas/phrmod/vendor/twig/twig/lib/Twig/ExpressionParser.php:436
 /Users/tomas/phrmod/vendor/twig/twig/lib/Twig/ExpressionParser.php:421
 /Users/tomas/phrmod/vendor/twig/twig/lib/Twig/ExpressionParser.php:285
 /Users/tomas/phrmod/vendor/twig/twig/lib/Twig/ExpressionParser.php:174
 /Users/tomas/phrmod/vendor/twig/twig/lib/Twig/ExpressionParser.php:84
 /Users/tomas/phrmod/vendor/twig/twig/lib/Twig/ExpressionParser.php:41
 /Users/tomas/phrmod/vendor/twig/twig/lib/Twig/Parser.php:145
 /Users/tomas/phrmod/vendor/twig/twig/lib/Twig/Parser.php:99
 /Users/tomas/phrmod/vendor/twig/twig/lib/Twig/Environment.php:479
 /Users/tomas/phrmod/vendor/twig/twig/lib/Twig/Environment.php:529
 /Users/tomas/phrmod/vendor/twig/twig/lib/Twig/Environment.php:320
 /Users/tomas/phrmod/Phrozn/Processor/Twig.php:78
 /Users/tomas/phrmod/tests/Phrozn/Processor/TwigTest.php:64

The filter raw is native in Twig since 1.10 http://twig.sensiolabs.org/doc/filters/raw.html and I can't explain to the rest of my team why is not working.

Any ideas how to make the raw filter work?

EDIT:

OK, I know now why it doesn't work.

    /**
     * Get (init if necessary) twig environment
     *
     * @param boolean $reset Force re-initialization (helpful for UTs)
     *
     * @return \Twig_Environment
     */
    protected function getEnvironment($reset = false)
    {
        if ($reset === true || null === $this->twig) {
            $this->twig = new \Twig_Environment(
                $this->getLoader(), $this->getConfig());
            $this->twig->removeExtension('escaper');
        }

        return $this->twig;
    }

Is there a reason why we would not need the escaper extension?

You could simple deactivate it by doing this:

    /**
     * Get (init if necessary) twig environment
     *
     * @param boolean $reset Force re-initialization (helpful for UTs)
     *
     * @return \Twig_Environment
     */
    protected function getEnvironment($reset = false)
    {
        if ($reset === true || null === $this->twig) {
            $options = $this->getConfig();

            $defaultOptions = array(
                'autoescape' => false, // false because other way Twig escapes all HTML in templates
            );

            $this->twig = new \Twig_Environment($this->getLoader(), array_merge($defaultOptions, $options));
        }

        return $this->twig;
    }
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant