Skip to content

Commit

Permalink
Merge pull request #21 from pattern-lab/fix-twig-embed
Browse files Browse the repository at this point in the history
Fix broken Twig embed
  • Loading branch information
dmolsen committed Apr 25, 2016
2 parents 8260750 + b890a3a commit 26c4a93
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace PatternLab\PatternEngine\Twig\Loaders;

use \PatternLab\Config;
use \PatternLab\PatternEngine\Twig\IncludeNodeVisitor;
use \PatternLab\PatternEngine\Twig\PatternDataNodeVisitor;
use \PatternLab\PatternEngine\Twig\Loaders\Twig\PatternPartialLoader as Twig_Loader_PatternPartialLoader;
use \PatternLab\PatternEngine\Twig\Loaders\Twig\PatternStringLoader as Twig_Loader_PatternStringLoader;
use \PatternLab\PatternEngine\Loader;
Expand Down Expand Up @@ -85,7 +85,7 @@ public function __construct($options = array()) {
$this->instance = TwigUtil::loadMacros($this->instance);

// add node visitor
$this->instance->addNodeVisitor(new IncludeNodeVisitor());
$this->instance->addNodeVisitor(new PatternDataNodeVisitor());
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/PatternLab/PatternEngine/Twig/PatternDataEmbedNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace PatternLab\PatternEngine\Twig;

class PatternDataEmbedNode extends \Twig_Node_Embed
{
use PatternDataNodeTrait;

public function __construct(\Twig_Node_Embed $originalNode, $data)
{
parent::__construct(
$originalNode->getAttribute('filename'),
$originalNode->getAttribute('index'),
$originalNode->getNode('variables'),
$originalNode->getAttribute('only'),
$originalNode->getAttribute('ignore_missing'),
$originalNode->getLine(),
$originalNode->getNodeTag()
);

$this->data = $data;
}
}
38 changes: 10 additions & 28 deletions src/PatternLab/PatternEngine/Twig/PatternDataIncludeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,19 @@

class PatternDataIncludeNode extends \Twig_Node_Include
{
protected $data;
use PatternDataNodeTrait;

public function __construct(\Twig_Node_Include $originalNode, $data)
{
\Twig_Node::__construct($originalNode->nodes, $originalNode->attributes, $originalNode->lineno, $originalNode->tag);
$this->data = $data;
}
parent::__construct(
$originalNode->getNode('expr'),
$originalNode->getNode('variables'),
$originalNode->getAttribute('only'),
$originalNode->getAttribute('ignore_missing'),
$originalNode->getLine(),
$originalNode->getNodeTag()
);

protected function addTemplateArguments(\Twig_Compiler $compiler)
{
if (null === $this->getNode('variables')) {
if (false === $this->getAttribute('only')) {
$compiler
->raw('array_merge($context, ')
->repr($this->data)
->raw(')')
;
}
else {
$compiler->raw('array()');
}
} elseif (false === $this->getAttribute('only')) {
$compiler
->raw('array_merge($context, ')
->repr($this->data)
->raw(', ')
->subcompile($this->getNode('variables'))
->raw(')')
;
} else {
$compiler->subcompile($this->getNode('variables'));
}
$this->data = $data;
}
}
34 changes: 34 additions & 0 deletions src/PatternLab/PatternEngine/Twig/PatternDataNodeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace PatternLab\PatternEngine\Twig;

trait PatternDataNodeTrait
{
protected $data;

protected function addTemplateArguments(\Twig_Compiler $compiler)
{
if (null === $this->getNode('variables')) {
if (false === $this->getAttribute('only')) {
$compiler
->raw('array_merge($context, ')
->repr($this->data)
->raw(')')
;
}
else {
$compiler->raw('array()');
}
} elseif (false === $this->getAttribute('only')) {
$compiler
->raw('array_merge($context, ')
->repr($this->data)
->raw(', ')
->subcompile($this->getNode('variables'))
->raw(')')
;
} else {
$compiler->subcompile($this->getNode('variables'));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PatternLab\Data;

class IncludeNodeVisitor extends \Twig_BaseNodeVisitor
class PatternDataNodeVisitor extends \Twig_BaseNodeVisitor
{
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
{
Expand All @@ -17,7 +17,12 @@ protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
if ($node->hasNode('expr') && $node->getNode('expr')->hasAttribute('value')) {
$patternStoreKey = $node->getNode('expr')->getAttribute('value');
$data = Data::getPatternSpecificData($patternStoreKey);
$dataNode = new PatternDataIncludeNode($node, $data);
if ($node instanceof \Twig_Node_Embed) {
$dataNode = new PatternDataEmbedNode($node, $data);
}
else {
$dataNode = new PatternDataIncludeNode($node, $data);
}

return $dataNode;
}
Expand Down

0 comments on commit 26c4a93

Please sign in to comment.