|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
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 namespace Symfony\Bundle\TwigBundle\TokenParser; |
|
13 |
|
14 use Symfony\Bundle\TwigBundle\Node\RenderNode; |
|
15 |
|
16 /** |
|
17 * |
|
18 * |
|
19 * @author Fabien Potencier <fabien@symfony.com> |
|
20 */ |
|
21 class RenderTokenParser extends \Twig_TokenParser |
|
22 { |
|
23 /** |
|
24 * Parses a token and returns a node. |
|
25 * |
|
26 * @param \Twig_Token $token A \Twig_Token instance |
|
27 * |
|
28 * @return \Twig_NodeInterface A \Twig_NodeInterface instance |
|
29 */ |
|
30 public function parse(\Twig_Token $token) |
|
31 { |
|
32 $expr = $this->parser->getExpressionParser()->parseExpression(); |
|
33 |
|
34 // attributes |
|
35 if ($this->parser->getStream()->test(\Twig_Token::NAME_TYPE, 'with')) { |
|
36 $this->parser->getStream()->next(); |
|
37 |
|
38 $hasAttributes = true; |
|
39 $attributes = $this->parser->getExpressionParser()->parseExpression(); |
|
40 } else { |
|
41 $hasAttributes = false; |
|
42 $attributes = new \Twig_Node_Expression_Array(array(), $token->getLine()); |
|
43 } |
|
44 |
|
45 // options |
|
46 if ($hasAttributes && $this->parser->getStream()->test(\Twig_Token::PUNCTUATION_TYPE, ',')) { |
|
47 $this->parser->getStream()->next(); |
|
48 |
|
49 $options = $this->parser->getExpressionParser()->parseExpression(); |
|
50 } else { |
|
51 $options = new \Twig_Node_Expression_Array(array(), $token->getLine()); |
|
52 } |
|
53 |
|
54 $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); |
|
55 |
|
56 return new RenderNode($expr, $attributes, $options, $token->getLine(), $this->getTag()); |
|
57 } |
|
58 |
|
59 /** |
|
60 * Gets the tag name associated with this token parser. |
|
61 * |
|
62 * @return string The tag name |
|
63 */ |
|
64 public function getTag() |
|
65 { |
|
66 return 'render'; |
|
67 } |
|
68 } |