Yaffs site version 1.1
[yaffs-website] / vendor / twig / twig / lib / Twig / TokenParser / Block.php
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
13 /**
14  * Marks a section of a template as being reusable.
15  *
16  * <pre>
17  *  {% block head %}
18  *    <link rel="stylesheet" href="style.css" />
19  *    <title>{% block title %}{% endblock %} - My Webpage</title>
20  *  {% endblock %}
21  * </pre>
22  *
23  * @final
24  */
25 class Twig_TokenParser_Block extends Twig_TokenParser
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         }
53         $stream->expect(Twig_Token::BLOCK_END_TYPE);
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);