JavascriptTokenParser.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Symfony\Bundle\TwigBundle\TokenParser;
  3. use Symfony\Bundle\TwigBundle\Node\JavascriptNode;
  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 JavascriptTokenParser 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. $attributes = $this->parser->getExpressionParser()->parseExpression();
  33. } else {
  34. $attributes = new \Twig_Node_Expression_Array(array(), $token->getLine());
  35. }
  36. $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
  37. return new JavascriptNode($expr, $attributes, $token->getLine(), $this->getTag());
  38. }
  39. /**
  40. * Gets the tag name associated with this token parser.
  41. *
  42. * @param string The tag name
  43. */
  44. public function getTag()
  45. {
  46. return 'javascript';
  47. }
  48. }