Import.php 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Imports macros.
  12. *
  13. * <pre>
  14. * {% import 'forms.html' as forms %}
  15. * </pre>
  16. */
  17. class Twig_TokenParser_Import 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. $macro = $this->parser->getExpressionParser()->parseExpression();
  29. $this->parser->getStream()->expect('as');
  30. $var = new Twig_Node_Expression_AssignName($this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue(), $token->getLine());
  31. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  32. return new Twig_Node_Import($macro, $var, $token->getLine(), $this->getTag());
  33. }
  34. /**
  35. * Gets the tag name associated with this token parser.
  36. *
  37. * @param string The tag name
  38. */
  39. public function getTag()
  40. {
  41. return 'import';
  42. }
  43. }