1
|
<?php
|
2
|
|
3
|
/*
|
4
|
* This file is part of Twig.
|
5
|
*
|
6
|
* (c) 2011 Fabien Potencier
|
7
|
*
|
8
|
* For the full copyright and license information, please view the LICENSE
|
9
|
* file that was distributed with this source code.
|
10
|
*/
|
11
|
|
12
|
/**
|
13
|
* Imports blocks defined in another template into the current template.
|
14
|
*
|
15
|
* <pre>
|
16
|
* {% extends "base.html" %}
|
17
|
*
|
18
|
* {% use "blocks.html" %}
|
19
|
*
|
20
|
* {% block title %}{% endblock %}
|
21
|
* {% block content %}{% endblock %}
|
22
|
* </pre>
|
23
|
*
|
24
|
* @see http://www.twig-project.org/doc/templates.html#horizontal-reuse for details.
|
25
|
*/
|
26
|
class Twig_TokenParser_Use extends Twig_TokenParser
|
27
|
{
|
28
|
public function parse(Twig_Token $token)
|
29
|
{
|
30
|
$template = $this->parser->getExpressionParser()->parseExpression();
|
31
|
$stream = $this->parser->getStream();
|
32
|
|
33
|
if (!$template instanceof Twig_Node_Expression_Constant) {
|
34
|
throw new Twig_Error_Syntax('The template references in a "use" statement must be a string.', $stream->getCurrent()->getLine(), $stream->getFilename());
|
35
|
}
|
36
|
|
37
|
$targets = array();
|
38
|
if ($stream->nextIf('with')) {
|
39
|
do {
|
40
|
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
|
41
|
|
42
|
$alias = $name;
|
43
|
if ($stream->nextIf('as')) {
|
44
|
$alias = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
|
45
|
}
|
46
|
|
47
|
$targets[$name] = new Twig_Node_Expression_Constant($alias, -1);
|
48
|
|
49
|
if (!$stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
|
50
|
break;
|
51
|
}
|
52
|
} while (true);
|
53
|
}
|
54
|
|
55
|
$stream->expect(Twig_Token::BLOCK_END_TYPE);
|
56
|
|
57
|
$this->parser->addTrait(new Twig_Node(array('template' => $template, 'targets' => new Twig_Node($targets))));
|
58
|
}
|
59
|
|
60
|
public function getTag()
|
61
|
{
|
62
|
return 'use';
|
63
|
}
|
64
|
}
|