Set.php 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010 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. * Represents a set node.
  12. *
  13. * @package twig
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Twig_Node_Set extends Twig_Node
  17. {
  18. public function __construct($capture, Twig_NodeInterface $names, Twig_NodeInterface $values, $lineno, $tag = null)
  19. {
  20. parent::__construct(array('names' => $names, 'values' => $values), array('capture' => $capture), $lineno, $tag);
  21. }
  22. /**
  23. * Compiles the node to PHP.
  24. *
  25. * @param Twig_Compiler A Twig_Compiler instance
  26. */
  27. public function compile(Twig_Compiler $compiler)
  28. {
  29. $compiler->addDebugInfo($this);
  30. if (count($this->getNode('names')) > 1) {
  31. $compiler->write('list(');
  32. foreach ($this->getNode('names') as $idx => $node) {
  33. if ($idx) {
  34. $compiler->raw(', ');
  35. }
  36. $compiler->subcompile($node);
  37. }
  38. $compiler->raw(')');
  39. } else {
  40. if ($this->getAttribute('capture')) {
  41. $compiler
  42. ->write("ob_start();\n")
  43. ->subcompile($this->getNode('values'))
  44. ;
  45. }
  46. $compiler->subcompile($this->getNode('names'), false);
  47. if ($this->getAttribute('capture')) {
  48. $compiler->raw(" = new Twig_Markup(ob_get_clean())");
  49. }
  50. }
  51. if (!$this->getAttribute('capture')) {
  52. $compiler->raw(' = ');
  53. if (count($this->getNode('names')) > 1) {
  54. $compiler->write('array(');
  55. foreach ($this->getNode('values') as $idx => $value) {
  56. if ($idx) {
  57. $compiler->raw(', ');
  58. }
  59. $compiler->subcompile($value);
  60. }
  61. $compiler->raw(')');
  62. } else {
  63. $compiler->subcompile($this->getNode('values'));
  64. }
  65. }
  66. $compiler->raw(";\n");
  67. }
  68. }