Include.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 include node.
  13. *
  14. * @package twig
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class Twig_Node_Include extends Twig_Node implements Twig_NodeOutputInterface
  18. {
  19. public function __construct(Twig_Node_Expression $expr, Twig_Node_Expression $variables = null, $only = false, $lineno, $tag = null)
  20. {
  21. parent::__construct(array('expr' => $expr, 'variables' => $variables), array('only' => (Boolean) $only), $lineno, $tag);
  22. }
  23. /**
  24. * Compiles the node to PHP.
  25. *
  26. * @param Twig_Compiler A Twig_Compiler instance
  27. */
  28. public function compile(Twig_Compiler $compiler)
  29. {
  30. $compiler->addDebugInfo($this);
  31. if ($this->getNode('expr') instanceof Twig_Node_Expression_Constant) {
  32. $compiler
  33. ->write("\$this->env->loadTemplate(")
  34. ->subcompile($this->getNode('expr'))
  35. ->raw(")->display(")
  36. ;
  37. } else {
  38. $compiler
  39. ->write("\$template = ")
  40. ->subcompile($this->getNode('expr'))
  41. ->raw(";\n")
  42. ->write("if (!\$template")
  43. ->raw(" instanceof Twig_Template) {\n")
  44. ->indent()
  45. ->write("\$template = \$this->env->loadTemplate(\$template);\n")
  46. ->outdent()
  47. ->write("}\n")
  48. ->write('$template->display(')
  49. ;
  50. }
  51. if (false === $this->getAttribute('only')) {
  52. if (null === $this->getNode('variables')) {
  53. $compiler->raw('$context');
  54. } else {
  55. $compiler
  56. ->raw('array_merge($context, ')
  57. ->subcompile($this->getNode('variables'))
  58. ->raw(')')
  59. ;
  60. }
  61. } else {
  62. if (null === $this->getNode('variables')) {
  63. $compiler->raw('array()');
  64. } else {
  65. $compiler->subcompile($this->getNode('variables'));
  66. }
  67. }
  68. $compiler->raw(");\n");
  69. }
  70. }