2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Cms\Twig;
|
|
|
|
|
|
|
|
use Twig_Node;
|
|
|
|
use Twig_Compiler;
|
|
|
|
use Twig_NodeInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a placeholder node
|
|
|
|
*
|
|
|
|
* @package october\cms
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
class PlaceholderNode extends Twig_Node
|
|
|
|
{
|
2014-10-16 20:47:23 -07:00
|
|
|
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
|
|
|
}
|
2014-10-16 20:47:23 -07:00
|
|
|
$attributes = $paramValues;
|
|
|
|
$attributes['name'] = $name;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
2014-10-16 20:47:23 -07: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();");
|
|
|
|
}
|
|
|
|
|
2014-10-16 20:47:23 -07:00
|
|
|
$isText = $this->hasAttribute('type') && $this->getAttribute('type') == 'text';
|
|
|
|
|
|
|
|
$compiler->addDebugInfo($this);
|
2014-10-18 11:06:54 +02:00
|
|
|
if (!$isText) {
|
2014-10-16 20:47:23 -07:00
|
|
|
$compiler->write("echo \$this->env->getExtension('CMS')->displayBlock(");
|
2014-11-01 12:00:45 +11:00
|
|
|
}
|
|
|
|
else {
|
2014-10-16 20:47:23 -07:00
|
|
|
$compiler->write("echo twig_escape_filter(\$this->env, \$this->env->getExtension('CMS')->displayBlock(");
|
2014-10-18 11:06:54 +02:00
|
|
|
}
|
2014-10-16 20:47:23 -07:00
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
$compiler
|
|
|
|
->raw("'".$this->getAttribute('name')."', ")
|
|
|
|
->raw("\$context[")
|
|
|
|
->raw("'".$varId."'")
|
|
|
|
->raw("]")
|
2014-10-16 20:47:23 -07:00
|
|
|
->raw(")");
|
|
|
|
|
2014-10-18 11:06:54 +02:00
|
|
|
if (!$isText) {
|
2014-10-16 20:47:23 -07:00
|
|
|
$compiler->raw(";\n");
|
2014-11-01 12:00:45 +11:00
|
|
|
}
|
|
|
|
else {
|
2014-10-16 20:47:23 -07:00
|
|
|
$compiler->raw(");\n");
|
2014-10-18 11:06:54 +02:00
|
|
|
}
|
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
|
|
|
}
|