4 * This file is part of Twig.
6 * (c) 2009 Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Marks a section of a template to be escaped or not.
16 * {% autoescape true %}
17 * Everything will be automatically escaped in this block
20 * {% autoescape false %}
21 * Everything will be outputed as is in this block
24 * {% autoescape true js %}
25 * Everything will be automatically escaped in this block
26 * using the js escaping strategy
30 class Twig_TokenParser_AutoEscape
extends Twig_TokenParser
33 * Parses a token and returns a node.
35 * @param Twig_Token $token A Twig_Token instance
37 * @return Twig_NodeInterface A Twig_NodeInterface instance
39 public function parse(Twig_Token
$token)
41 $lineno = $token->getLine();
42 $stream = $this->parser
->getStream();
44 if ($stream->test(Twig_Token
::BLOCK_END_TYPE
)) {
47 $expr = $this->parser
->getExpressionParser()->parseExpression();
48 if (!$expr instanceof Twig_Node_Expression_Constant
) {
49 throw new Twig_Error_Syntax('An escaping strategy must be a string or a Boolean.', $stream->getCurrent()->getLine(), $stream->getFilename());
51 $value = $expr->getAttribute('value');
53 $compat = true === $value || false === $value;
55 if (true === $value) {
59 if ($compat && $stream->test(Twig_Token
::NAME_TYPE
)) {
60 if (false === $value) {
61 throw new Twig_Error_Syntax('Unexpected escaping strategy as you set autoescaping to false.', $stream->getCurrent()->getLine(), $stream->getFilename());
64 $value = $stream->next()->getValue();
68 $stream->expect(Twig_Token
::BLOCK_END_TYPE
);
69 $body = $this->parser
->subparse(array($this, 'decideBlockEnd'), true);
70 $stream->expect(Twig_Token
::BLOCK_END_TYPE
);
72 return new Twig_Node_AutoEscape($value, $body, $lineno, $this->getTag());
75 public function decideBlockEnd(Twig_Token
$token)
77 return $token->test('endautoescape');
81 * Gets the tag name associated with this token parser.
83 * @return string The tag name
85 public function getTag()