IncludeTokenParser.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Symfony\Bundle\TwigBundle\TokenParser;
  3. use Symfony\Bundle\TwigBundle\Node\IncludeNode;
  4. /*
  5. * This file is part of the Symfony package.
  6. *
  7. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  8. *
  9. * For the full copyright and license information, please view the LICENSE
  10. * file that was distributed with this source code.
  11. */
  12. /**
  13. *
  14. *
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. class IncludeTokenParser 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. $variables = null;
  30. if ($this->parser->getStream()->test(\Twig_Token::NAME_TYPE, 'with')) {
  31. $this->parser->getStream()->next();
  32. $variables = $this->parser->getExpressionParser()->parseExpression();
  33. }
  34. $only = false;
  35. if ($this->parser->getStream()->test(\Twig_Token::NAME_TYPE, 'only')) {
  36. $this->parser->getStream()->next();
  37. $only = true;
  38. }
  39. $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
  40. return new IncludeNode($expr, $variables, $only, $token->getLine(), $this->getTag());
  41. }
  42. /**
  43. * Gets the tag name associated with this token parser.
  44. *
  45. * @param string The tag name
  46. */
  47. public function getTag()
  48. {
  49. return 'include';
  50. }
  51. }