For.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. * (c) 2009 Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Loops over each item of a sequence.
  13. *
  14. * <pre>
  15. * <ul>
  16. * {% for user in users %}
  17. * <li>{{ user.username|e }}</li>
  18. * {% endfor %}
  19. * </ul>
  20. * </pre>
  21. */
  22. class Twig_TokenParser_For extends Twig_TokenParser
  23. {
  24. /**
  25. * Parses a token and returns a node.
  26. *
  27. * @param Twig_Token $token A Twig_Token instance
  28. *
  29. * @return Twig_NodeInterface A Twig_NodeInterface instance
  30. */
  31. public function parse(Twig_Token $token)
  32. {
  33. $lineno = $token->getLine();
  34. $targets = $this->parser->getExpressionParser()->parseAssignmentExpression();
  35. $this->parser->getStream()->expect(Twig_Token::OPERATOR_TYPE, 'in');
  36. $seq = $this->parser->getExpressionParser()->parseExpression();
  37. $ifexpr = null;
  38. if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE, 'if')) {
  39. $this->parser->getStream()->next();
  40. $ifexpr = $this->parser->getExpressionParser()->parseExpression();
  41. }
  42. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  43. $body = $this->parser->subparse(array($this, 'decideForFork'));
  44. if ($this->parser->getStream()->next()->getValue() == 'else') {
  45. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  46. $else = $this->parser->subparse(array($this, 'decideForEnd'), true);
  47. } else {
  48. $else = null;
  49. }
  50. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  51. if (count($targets) > 1) {
  52. $keyTarget = $targets->getNode(0);
  53. $keyTarget = new Twig_Node_Expression_AssignName($keyTarget->getAttribute('name'), $keyTarget->getLine());
  54. $valueTarget = $targets->getNode(1);
  55. $valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine());
  56. } else {
  57. $keyTarget = new Twig_Node_Expression_AssignName('_key', $lineno);
  58. $valueTarget = $targets->getNode(0);
  59. $valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine());
  60. }
  61. return new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, $lineno, $this->getTag());
  62. }
  63. public function decideForFork(Twig_Token $token)
  64. {
  65. return $token->test(array('else', 'endfor'));
  66. }
  67. public function decideForEnd(Twig_Token $token)
  68. {
  69. return $token->test('endfor');
  70. }
  71. /**
  72. * Gets the tag name associated with this token parser.
  73. *
  74. * @return string The tag name
  75. */
  76. public function getTag()
  77. {
  78. return 'for';
  79. }
  80. }