winter/modules/cms/twig/ComponentNode.php

46 lines
1.3 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace Cms\Twig;
use Twig_Node;
use Twig_Compiler;
use Twig_NodeInterface;
2014-05-14 23:24:20 +10:00
/**
* Represents a component node
*
* @package october\cms
* @author Alexey Bobkov, Samuel Georges
*/
class ComponentNode extends Twig_Node
{
public function __construct(Twig_NodeInterface $nodes, $paramNames, $lineno, $tag = 'component')
2014-05-14 23:24:20 +10:00
{
parent::__construct(['nodes' => $nodes], ['names' => $paramNames], $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)
{
$compiler->addDebugInfo($this);
$compiler->write("\$context['__cms_component_params'] = [];\n");
for ($i = 1; $i < count($this->getNode('nodes')); $i++) {
$compiler->write("\$context['__cms_component_params']['".$this->getAttribute('names')[$i-1]."'] = ");
$compiler->subcompile($this->getNode('nodes')->getNode($i));
$compiler->write(";\n");
}
2014-05-14 23:24:20 +10:00
$compiler
->write("echo \$this->env->getExtension('CMS')->componentFunction(")
->subcompile($this->getNode('nodes')->getNode(0))
->write(", \$context['__cms_component_params']")
2014-05-14 23:24:20 +10:00
->write(");\n")
;
$compiler->write("unset(\$context['__cms_component_params']);\n");
2014-05-14 23:24:20 +10:00
}
}