ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
If.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  * (c) Armin Ronacher
8  *
9  * For the full copyright and license information, please view the LICENSE
10  * file that was distributed with this source code.
11  */
12 
29 {
30  public function parse(Twig_Token $token)
31  {
32  $lineno = $token->getLine();
33  $expr = $this->parser->getExpressionParser()->parseExpression();
34  $stream = $this->parser->getStream();
36  $body = $this->parser->subparse(array($this, 'decideIfFork'));
37  $tests = array($expr, $body);
38  $else = null;
39 
40  $end = false;
41  while (!$end) {
42  switch ($stream->next()->getValue()) {
43  case 'else':
45  $else = $this->parser->subparse(array($this, 'decideIfEnd'));
46  break;
47 
48  case 'elseif':
49  $expr = $this->parser->getExpressionParser()->parseExpression();
51  $body = $this->parser->subparse(array($this, 'decideIfFork'));
52  $tests[] = $expr;
53  $tests[] = $body;
54  break;
55 
56  case 'endif':
57  $end = true;
58  break;
59 
60  default:
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());
62  }
63  }
64 
66 
67  return new Twig_Node_If(new Twig_Node($tests), $else, $lineno, $this->getTag());
68  }
69 
70  public function decideIfFork(Twig_Token $token)
71  {
72  return $token->test(array('elseif', 'else', 'endif'));
73  }
74 
75  public function decideIfEnd(Twig_Token $token)
76  {
77  return $token->test(array('endif'));
78  }
79 
80  public function getTag()
81  {
82  return 'if';
83  }
84 }
85 
86 class_alias('Twig_TokenParser_If', 'Twig\TokenParser\IfTokenParser', false);
Represents an if node.
Definition: If.php:18
Represents a node in the AST.
Definition: Node.php:18
Tests a condition.
Definition: If.php:28
test($type, $values=null)
Tests the current token for a type and/or a value.
Definition: Token.php:70
$stream
PHP stream implementation.
Exception thrown when a syntax error occurs during lexing or parsing of a template.
Definition: Syntax.php:18
decideIfEnd(Twig_Token $token)
Definition: If.php:75
getTag()
Gets the tag name associated with this token parser.
Definition: If.php:80
Base class for all token parsers.
Definition: TokenParser.php:17
$tests
Definition: bench.php:104
decideIfFork(Twig_Token $token)
Definition: If.php:70
parse(Twig_Token $token)
Parses a token and returns a node.
Definition: If.php:30
getLine()
Definition: Token.php:87
const BLOCK_END_TYPE
Definition: Token.php:30