Include.php (2502B)
1 <?php 2 3 /* 4 * This file is part of Twig. 5 * 6 * (c) 2009 Fabien Potencier 7 * (c) 2009 Armin Ronacher 8 * 9 * For the full copyright and license information, please view the LICENSE 10 * file that was distributed with this source code. 11 */ 12 13 /** 14 * Represents an include node. 15 * 16 * @author Fabien Potencier <fabien@symfony.com> 17 */ 18 class Twig_Node_Include extends Twig_Node implements Twig_NodeOutputInterface 19 { 20 public function __construct(Twig_Node_Expression $expr, Twig_Node_Expression $variables = null, $only = false, $ignoreMissing = false, $lineno, $tag = null) 21 { 22 parent::__construct(array('expr' => $expr, 'variables' => $variables), array('only' => (bool) $only, 'ignore_missing' => (bool) $ignoreMissing), $lineno, $tag); 23 } 24 25 public function compile(Twig_Compiler $compiler) 26 { 27 $compiler->addDebugInfo($this); 28 29 if ($this->getAttribute('ignore_missing')) { 30 $compiler 31 ->write("try {\n") 32 ->indent() 33 ; 34 } 35 36 $this->addGetTemplate($compiler); 37 38 $compiler->raw('->display('); 39 40 $this->addTemplateArguments($compiler); 41 42 $compiler->raw(");\n"); 43 44 if ($this->getAttribute('ignore_missing')) { 45 $compiler 46 ->outdent() 47 ->write("} catch (Twig_Error_Loader \$e) {\n") 48 ->indent() 49 ->write("// ignore missing template\n") 50 ->outdent() 51 ->write("}\n\n") 52 ; 53 } 54 } 55 56 protected function addGetTemplate(Twig_Compiler $compiler) 57 { 58 $compiler 59 ->write('$this->loadTemplate(') 60 ->subcompile($this->getNode('expr')) 61 ->raw(', ') 62 ->repr($compiler->getFilename()) 63 ->raw(', ') 64 ->repr($this->getLine()) 65 ->raw(')') 66 ; 67 } 68 69 protected function addTemplateArguments(Twig_Compiler $compiler) 70 { 71 if (null === $this->getNode('variables')) { 72 $compiler->raw(false === $this->getAttribute('only') ? '$context' : 'array()'); 73 } elseif (false === $this->getAttribute('only')) { 74 $compiler 75 ->raw('array_merge($context, ') 76 ->subcompile($this->getNode('variables')) 77 ->raw(')') 78 ; 79 } else { 80 $compiler->subcompile($this->getNode('variables')); 81 } 82 } 83 }