mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-23 08:38:45 +00:00
189 lines
5.2 KiB
PHP
189 lines
5.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Twig.
|
|
*
|
|
* (c) 2009 Fabien Potencier
|
|
* (c) 2009 Armin Ronacher
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
/**
|
|
* Represents a module node.
|
|
*
|
|
* @package twig
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
* @version SVN: $Id$
|
|
*/
|
|
class Twig_Node_Module extends Twig_Node
|
|
{
|
|
public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $parent = null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, $filename)
|
|
{
|
|
parent::__construct(array('parent' => $parent, 'body' => $body, 'blocks' => $blocks, 'macros' => $macros), array('filename' => $filename), 1);
|
|
}
|
|
|
|
public function compile($compiler)
|
|
{
|
|
$this->compileTemplate($compiler);
|
|
$this->compileMacros($compiler);
|
|
}
|
|
|
|
protected function compileTemplate($compiler)
|
|
{
|
|
$this->compileClassHeader($compiler);
|
|
|
|
if (count($this->blocks)) {
|
|
$this->compileConstructor($compiler);
|
|
}
|
|
|
|
$this->compileDisplayHeader($compiler);
|
|
|
|
$this->compileDisplayBody($compiler);
|
|
|
|
$this->compileDisplayFooter($compiler);
|
|
|
|
$compiler->subcompile($this->blocks);
|
|
|
|
$this->compileGetName($compiler);
|
|
|
|
$this->compileClassFooter($compiler);
|
|
}
|
|
|
|
protected function compileDisplayBody($compiler)
|
|
{
|
|
if (null !== $this->parent) {
|
|
// remove all but import nodes
|
|
foreach ($this->body as $node) {
|
|
if ($node instanceof Twig_Node_Import) {
|
|
$compiler->subcompile($node);
|
|
}
|
|
}
|
|
|
|
if ($this->parent instanceof Twig_Node_Expression_Constant) {
|
|
$compiler
|
|
->write("\$this->parent = \$this->env->loadTemplate(")
|
|
->subcompile($this->parent)
|
|
->raw(");\n")
|
|
;
|
|
} else {
|
|
$compiler
|
|
->write("\$this->parent = ")
|
|
->subcompile($this->parent)
|
|
->raw(";\n")
|
|
->write("if (!\$this->parent")
|
|
->raw(" instanceof Twig_Template) {\n")
|
|
->indent()
|
|
->write("\$this->parent = \$this->env->loadTemplate(\$this->parent);\n")
|
|
->outdent()
|
|
->write("}\n")
|
|
;
|
|
}
|
|
|
|
$compiler
|
|
->write("\$this->parent->pushBlocks(\$this->blocks);\n")
|
|
->write("\$this->parent->display(\$context);\n")
|
|
;
|
|
} else {
|
|
$compiler->subcompile($this->body);
|
|
}
|
|
}
|
|
|
|
protected function compileClassHeader($compiler)
|
|
{
|
|
$compiler
|
|
->write("<?php\n\n")
|
|
// if the filename contains */, add a blank to avoid a PHP parse error
|
|
->write("/* ".str_replace('*/', '* /', $this['filename'])." */\n")
|
|
->write('class '.$compiler->getEnvironment()->getTemplateClass($this['filename']))
|
|
->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass()))
|
|
->write("{\n")
|
|
->indent()
|
|
;
|
|
|
|
if (null !== $this->parent) {
|
|
$compiler->write("protected \$parent;\n\n");
|
|
}
|
|
}
|
|
|
|
protected function compileConstructor($compiler)
|
|
{
|
|
$compiler
|
|
->write("public function __construct(Twig_Environment \$env)\n", "{\n")
|
|
->indent()
|
|
->write("parent::__construct(\$env);\n\n")
|
|
->write("\$this->blocks = array(\n")
|
|
->indent()
|
|
;
|
|
|
|
foreach ($this->blocks as $name => $node) {
|
|
$compiler
|
|
->write(sprintf("'%s' => array(array(\$this, 'block_%s')),\n", $name, $name))
|
|
;
|
|
}
|
|
|
|
$compiler
|
|
->outdent()
|
|
->write(");\n")
|
|
->outdent()
|
|
->write("}\n\n");
|
|
;
|
|
}
|
|
|
|
protected function compileDisplayHeader($compiler)
|
|
{
|
|
$compiler
|
|
->write("public function display(array \$context)\n", "{\n")
|
|
->indent()
|
|
;
|
|
}
|
|
|
|
protected function compileGetName($compiler)
|
|
{
|
|
$compiler
|
|
->write("public function getName()\n", "{\n")
|
|
->indent()
|
|
->write('return ')
|
|
->string($this['filename'])
|
|
->raw(";\n")
|
|
->outdent()
|
|
->write("}\n\n")
|
|
;
|
|
}
|
|
|
|
protected function compileDisplayFooter($compiler)
|
|
{
|
|
$compiler
|
|
->outdent()
|
|
->write("}\n\n")
|
|
;
|
|
}
|
|
|
|
protected function compileClassFooter($compiler)
|
|
{
|
|
$compiler
|
|
->outdent()
|
|
->write("}\n")
|
|
;
|
|
}
|
|
|
|
protected function compileMacros($compiler)
|
|
{
|
|
$compiler
|
|
->write("\n")
|
|
->write('class '.$compiler->getEnvironment()->getTemplateClass($this['filename']).'_Macro extends Twig_Macro'."\n")
|
|
->write("{\n")
|
|
->indent()
|
|
;
|
|
|
|
// macros
|
|
$compiler->subcompile($this->macros);
|
|
|
|
$compiler
|
|
->outdent()
|
|
->write("}\n")
|
|
;
|
|
}
|
|
}
|