If.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. * Represents an if node.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Twig_Node_If extends Twig_Node
  17. {
  18. public function __construct(Twig_NodeInterface $tests, Twig_NodeInterface $else = null, $lineno, $tag = null)
  19. {
  20. parent::__construct(array('tests' => $tests, 'else' => $else), array(), $lineno, $tag);
  21. }
  22. public function compile(Twig_Compiler $compiler)
  23. {
  24. $compiler->addDebugInfo($this);
  25. for ($i = 0, $count = count($this->getNode('tests')); $i < $count; $i += 2) {
  26. if ($i > 0) {
  27. $compiler
  28. ->outdent()
  29. ->write('} elseif (')
  30. ;
  31. } else {
  32. $compiler
  33. ->write('if (')
  34. ;
  35. }
  36. $compiler
  37. ->subcompile($this->getNode('tests')->getNode($i))
  38. ->raw(") {\n")
  39. ->indent()
  40. ->subcompile($this->getNode('tests')->getNode($i + 1))
  41. ;
  42. }
  43. if ($this->hasNode('else') && null !== $this->getNode('else')) {
  44. $compiler
  45. ->outdent()
  46. ->write("} else {\n")
  47. ->indent()
  48. ->subcompile($this->getNode('else'))
  49. ;
  50. }
  51. $compiler
  52. ->outdent()
  53. ->write("}\n");
  54. }
  55. }