winter/modules/cms/twig/PlaceholderNode.php

82 lines
2.2 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace Cms\Twig;
use Twig_Node;
use Twig_Compiler;
/**
* Represents a placeholder node
*
* @package october\cms
* @author Alexey Bobkov, Samuel Georges
*/
class PlaceholderNode extends Twig_Node
{
public function __construct($name, $paramValues, $body, $lineno, $tag = 'placeholder')
2014-05-14 23:24:20 +10:00
{
$nodes = [];
2014-10-11 01:42:04 +02:00
if ($body) {
2014-05-14 23:24:20 +10:00
$nodes['default'] = $body;
2014-10-11 01:42:04 +02:00
}
$attributes = $paramValues;
$attributes['name'] = $name;
2014-05-14 23:24:20 +10:00
parent::__construct($nodes, $attributes, $lineno, $tag);
2014-05-14 23:24:20 +10:00
}
/**
* Compiles the node to PHP.
*
2014-05-17 18:08:01 +02:00
* @param Twig_Compiler $compiler A Twig_Compiler instance
2014-05-14 23:24:20 +10:00
*/
public function compile(Twig_Compiler $compiler)
{
$hasBody = $this->hasNode('default');
$varId = '__placeholder_'.$this->getAttribute('name').'_default_contents';
$compiler
->addDebugInfo($this)
->write("\$context[")
->raw("'".$varId."'")
->raw("] = null;");
if ($hasBody) {
$compiler
->addDebugInfo($this)
->write('ob_start();')
->subcompile($this->getNode('default'))
->write("\$context[")
->raw("'".$varId."'")
->raw("] = ob_get_clean();");
}
$isText = $this->hasAttribute('type') && $this->getAttribute('type') == 'text';
$compiler->addDebugInfo($this);
if (!$isText) {
2017-05-13 09:34:20 +10:00
$compiler->write("echo \$this->env->getExtension('Cms\Twig\Extension')->displayBlock(");
}
else {
2017-05-13 09:34:20 +10:00
$compiler->write("echo twig_escape_filter(\$this->env, \$this->env->getExtension('Cms\Twig\Extension')->displayBlock(");
}
2014-05-14 23:24:20 +10:00
$compiler
->raw("'".$this->getAttribute('name')."', ")
->raw("\$context[")
->raw("'".$varId."'")
->raw("]")
->raw(")");
if (!$isText) {
$compiler->raw(";\n");
}
else {
$compiler->raw(");\n");
}
2014-05-14 23:24:20 +10:00
$compiler
->addDebugInfo($this)
->write("unset(\$context[")
->raw("'".$varId."'")
->raw("]);");
}
2014-10-11 01:42:04 +02:00
}