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.
14 * Marks a section of a template as being reusable.
18 * <link rel="stylesheet" href="style.css" />
19 * <title>{% block title %}{% endblock %} - My Webpage</title>
25 class Twig_TokenParser_Block extends Twig_TokenParser
27 public function parse(Twig_Token $token)
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());
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);
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();
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());
49 $body = new Twig_Node(array(
50 new Twig_Node_Print($this->parser->getExpressionParser()->parseExpression(), $lineno),
53 $stream->expect(Twig_Token::BLOCK_END_TYPE);
55 $block->setNode('body', $body);
56 $this->parser->popBlockStack();
57 $this->parser->popLocalScope();
59 return new Twig_Node_BlockReference($name, $lineno, $this->getTag());
62 public function decideBlockEnd(Twig_Token $token)
64 return $token->test('endblock');
67 public function getTag()
73 class_alias('Twig_TokenParser_Block', 'Twig\TokenParser\BlockTokenParser', false);