Skip to content

Commit

Permalink
fixed "element" token
Browse files Browse the repository at this point in the history
  • Loading branch information
m3nt0r committed Feb 20, 2012
1 parent d67e5e4 commit bf883a3
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions libs/element_token.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,34 @@ public function getTag()
*/
class Twig_Node_Element extends Twig_Node
{
public function __construct(Twig_Node_Expression $expr, Twig_Node_Expression $variables = null, $lineno, $tag = null)
public function __construct(Twig_Node_Expression $expr, Twig_Node_Expression $variables = null, $only = false, $lineno, $tag = null)
{
parent::__construct(array('expr' => $expr, 'variables' => $variables), array(), $lineno, $tag);
parent::__construct(array('expr' => $expr, 'variables' => $variables), array('only' => (Boolean) $only), $lineno, $tag);
}

/**
* Compiles the node to PHP.
*
* @param Twig_Compiler A Twig_Compiler instance
*/
function compile(Twig_Compiler $compiler)
public function compile(Twig_Compiler $compiler)
{
$compiler->addDebugInfo($this);

if ($this->expr instanceof Twig_Node_Expression_Constant) {

$value = 'elements' . DS . $this->expr->offsetGet('value') . '.tpl';
$this->expr->offsetSet('value', $value);

$template = $this->getNode('expr')->getAttribute('value');
$value = 'elements' . DS . $template . '.tpl'; // prefix elements path
$this->getNode('expr')->setAttribute('value', $value);
if ($this->getNode('expr') instanceof Twig_Node_Expression_Constant) {
$compiler
->write("\$this->env->loadTemplate(")
->subcompile($this->expr)
->subcompile($this->getNode('expr'))
->raw(")->display(")
;
} else {
$compiler
->write("\$template = ")
->subcompile($this->expr)
->subcompile($this->getNode('expr'))
->raw(";\n")
->write("if (!\$template")
->raw(" instanceof Twig_Template) {\n")
Expand All @@ -103,10 +103,22 @@ function compile(Twig_Compiler $compiler)
;
}

if (null === $this->variables) {
$compiler->raw('$context');
if (false === $this->getAttribute('only')) {
if (null === $this->getNode('variables')) {
$compiler->raw('$context');
} else {
$compiler
->raw('array_merge($context, ')
->subcompile($this->getNode('variables'))
->raw(')')
;
}
} else {
$compiler->subcompile($this->variables);
if (null === $this->getNode('variables')) {
$compiler->raw('array()');
} else {
$compiler->subcompile($this->getNode('variables'));
}
}

$compiler->raw(");\n");
Expand Down

0 comments on commit bf883a3

Please sign in to comment.