Import.php 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. $this->parser->addImportedSymbol('template', $var->getAttribute('name'));
  33. return new Twig_Node_Import($macro, $var, $token->getLine(), $this->getTag());
  34. }
  35. /**
  36. * Gets the tag name associated with this token parser.
  37. *
  38. * @return string The tag name
  39. */
  40. public function getTag()
  41. {
  42. return 'import';
  43. }
  44. }