4 * This file is part of Twig.
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
19 * {% for user in users %}
20 * <li>{{ user.username|e }}</li>
28 class Twig_TokenParser_If extends Twig_TokenParser
30 public function parse(Twig_Token $token)
32 $lineno = $token->getLine();
33 $expr = $this->parser->getExpressionParser()->parseExpression();
34 $stream = $this->parser->getStream();
35 $stream->expect(Twig_Token::BLOCK_END_TYPE);
36 $body = $this->parser->subparse(array($this, 'decideIfFork'));
37 $tests = array($expr, $body);
42 switch ($stream->next()->getValue()) {
44 $stream->expect(Twig_Token::BLOCK_END_TYPE);
45 $else = $this->parser->subparse(array($this, 'decideIfEnd'));
49 $expr = $this->parser->getExpressionParser()->parseExpression();
50 $stream->expect(Twig_Token::BLOCK_END_TYPE);
51 $body = $this->parser->subparse(array($this, 'decideIfFork'));
61 throw new Twig_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "else", "elseif", or "endif" to close the "if" block started at line %d).', $lineno), $stream->getCurrent()->getLine(), $stream->getSourceContext());
65 $stream->expect(Twig_Token::BLOCK_END_TYPE);
67 return new Twig_Node_If(new Twig_Node($tests), $else, $lineno, $this->getTag());
70 public function decideIfFork(Twig_Token $token)
72 return $token->test(array('elseif', 'else', 'endif'));
75 public function decideIfEnd(Twig_Token $token)
77 return $token->test(array('endif'));
80 public function getTag()
86 class_alias('Twig_TokenParser_If', 'Twig\TokenParser\IfTokenParser', false);