|
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 /** |
|
29 * Parses a token and returns a node. |
|
30 * |
|
31 * @param Twig_Token $token A Twig_Token instance |
|
32 * |
|
33 * @return Twig_NodeInterface A Twig_NodeInterface instance |
|
34 */ |
|
35 public function parse(Twig_Token $token) |
|
36 { |
|
37 $template = $this->parser->getExpressionParser()->parseExpression(); |
|
38 |
|
39 if (!$template instanceof Twig_Node_Expression_Constant) { |
|
40 throw new Twig_Error_Syntax('The template references in a "use" statement must be a string.', $token->getLine()); |
|
41 } |
|
42 |
|
43 $stream = $this->parser->getStream(); |
|
44 |
|
45 $targets = array(); |
|
46 if ($stream->test('with')) { |
|
47 $stream->next(); |
|
48 |
|
49 do { |
|
50 $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); |
|
51 |
|
52 $alias = $name; |
|
53 if ($stream->test('as')) { |
|
54 $stream->next(); |
|
55 |
|
56 $alias = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); |
|
57 } |
|
58 |
|
59 $targets[$name] = new Twig_Node_Expression_Constant($alias, -1); |
|
60 |
|
61 if (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ',')) { |
|
62 break; |
|
63 } |
|
64 |
|
65 $stream->next(); |
|
66 } while (true); |
|
67 } |
|
68 |
|
69 $stream->expect(Twig_Token::BLOCK_END_TYPE); |
|
70 |
|
71 $this->parser->addTrait(new Twig_Node(array('template' => $template, 'targets' => new Twig_Node($targets)))); |
|
72 |
|
73 return null; |
|
74 } |
|
75 |
|
76 /** |
|
77 * Gets the tag name associated with this token parser. |
|
78 * |
|
79 * @param string The tag name |
|
80 */ |
|
81 public function getTag() |
|
82 { |
|
83 return 'use'; |
|
84 } |
|
85 } |