RenderTokenParser.php 1.9 KB

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