ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Block.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 
26 {
27  public function parse(Twig_Token $token)
28  {
29  $lineno = $token->getLine();
30  $stream = $this->parser->getStream();
31  $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
32  if ($this->parser->hasBlock($name)) {
33  throw new Twig_Error_Syntax(sprintf("The block '%s' has already been defined line %d.", $name, $this->parser->getBlock($name)->getTemplateLine()), $stream->getCurrent()->getLine(), $stream->getSourceContext());
34  }
35  $this->parser->setBlock($name, $block = new Twig_Node_Block($name, new Twig_Node(array()), $lineno));
36  $this->parser->pushLocalScope();
37  $this->parser->pushBlockStack($name);
38 
39  if ($stream->nextIf(Twig_Token::BLOCK_END_TYPE)) {
40  $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
41  if ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) {
42  $value = $token->getValue();
43 
44  if ($value != $name) {
45  throw new Twig_Error_Syntax(sprintf('Expected endblock for block "%s" (but "%s" given).', $name, $value), $stream->getCurrent()->getLine(), $stream->getSourceContext());
46  }
47  }
48  } else {
49  $body = new Twig_Node(array(
50  new Twig_Node_Print($this->parser->getExpressionParser()->parseExpression(), $lineno),
51  ));
52  }
54 
55  $block->setNode('body', $body);
56  $this->parser->popBlockStack();
57  $this->parser->popLocalScope();
58 
59  return new Twig_Node_BlockReference($name, $lineno, $this->getTag());
60  }
61 
62  public function decideBlockEnd(Twig_Token $token)
63  {
64  return $token->test('endblock');
65  }
66 
67  public function getTag()
68  {
69  return 'block';
70  }
71 }
72 
73 class_alias('Twig_TokenParser_Block', 'Twig\TokenParser\BlockTokenParser', false);
Marks a section of a template as being reusable.
Definition: Block.php:25
Represents a node in the AST.
Definition: Node.php:18
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
Represents a block node.
Definition: Block.php:18
Represents a block call node.
Represents a node that outputs an expression.
Definition: Print.php:18
if($format !==null) $name
Definition: metadata.php:146
Base class for all token parsers.
Definition: TokenParser.php:17
getValue()
Definition: Token.php:103
parse(Twig_Token $token)
Parses a token and returns a node.
Definition: Block.php:27
decideBlockEnd(Twig_Token $token)
Definition: Block.php:62
Create styles array
The data for the language used.
getTag()
Gets the tag name associated with this token parser.
Definition: Block.php:67
getLine()
Definition: Token.php:87
const NAME_TYPE
Definition: Token.php:32
const BLOCK_END_TYPE
Definition: Token.php:30