Skip to content

Commit 26c4a93

Browse files
committed
Merge pull request #21 from pattern-lab/fix-twig-embed
Fix broken Twig embed
2 parents 8260750 + b890a3a commit 26c4a93

File tree

5 files changed

+76
-32
lines changed

5 files changed

+76
-32
lines changed

Diff for: src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace PatternLab\PatternEngine\Twig\Loaders;
1515

1616
use \PatternLab\Config;
17-
use \PatternLab\PatternEngine\Twig\IncludeNodeVisitor;
17+
use \PatternLab\PatternEngine\Twig\PatternDataNodeVisitor;
1818
use \PatternLab\PatternEngine\Twig\Loaders\Twig\PatternPartialLoader as Twig_Loader_PatternPartialLoader;
1919
use \PatternLab\PatternEngine\Twig\Loaders\Twig\PatternStringLoader as Twig_Loader_PatternStringLoader;
2020
use \PatternLab\PatternEngine\Loader;
@@ -85,7 +85,7 @@ public function __construct($options = array()) {
8585
$this->instance = TwigUtil::loadMacros($this->instance);
8686

8787
// add node visitor
88-
$this->instance->addNodeVisitor(new IncludeNodeVisitor());
88+
$this->instance->addNodeVisitor(new PatternDataNodeVisitor());
8989
}
9090

9191
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace PatternLab\PatternEngine\Twig;
4+
5+
class PatternDataEmbedNode extends \Twig_Node_Embed
6+
{
7+
use PatternDataNodeTrait;
8+
9+
public function __construct(\Twig_Node_Embed $originalNode, $data)
10+
{
11+
parent::__construct(
12+
$originalNode->getAttribute('filename'),
13+
$originalNode->getAttribute('index'),
14+
$originalNode->getNode('variables'),
15+
$originalNode->getAttribute('only'),
16+
$originalNode->getAttribute('ignore_missing'),
17+
$originalNode->getLine(),
18+
$originalNode->getNodeTag()
19+
);
20+
21+
$this->data = $data;
22+
}
23+
}

Diff for: src/PatternLab/PatternEngine/Twig/PatternDataIncludeNode.php

+10-28
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,19 @@
44

55
class PatternDataIncludeNode extends \Twig_Node_Include
66
{
7-
protected $data;
7+
use PatternDataNodeTrait;
88

99
public function __construct(\Twig_Node_Include $originalNode, $data)
1010
{
11-
\Twig_Node::__construct($originalNode->nodes, $originalNode->attributes, $originalNode->lineno, $originalNode->tag);
12-
$this->data = $data;
13-
}
11+
parent::__construct(
12+
$originalNode->getNode('expr'),
13+
$originalNode->getNode('variables'),
14+
$originalNode->getAttribute('only'),
15+
$originalNode->getAttribute('ignore_missing'),
16+
$originalNode->getLine(),
17+
$originalNode->getNodeTag()
18+
);
1419

15-
protected function addTemplateArguments(\Twig_Compiler $compiler)
16-
{
17-
if (null === $this->getNode('variables')) {
18-
if (false === $this->getAttribute('only')) {
19-
$compiler
20-
->raw('array_merge($context, ')
21-
->repr($this->data)
22-
->raw(')')
23-
;
24-
}
25-
else {
26-
$compiler->raw('array()');
27-
}
28-
} elseif (false === $this->getAttribute('only')) {
29-
$compiler
30-
->raw('array_merge($context, ')
31-
->repr($this->data)
32-
->raw(', ')
33-
->subcompile($this->getNode('variables'))
34-
->raw(')')
35-
;
36-
} else {
37-
$compiler->subcompile($this->getNode('variables'));
38-
}
20+
$this->data = $data;
3921
}
4022
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace PatternLab\PatternEngine\Twig;
4+
5+
trait PatternDataNodeTrait
6+
{
7+
protected $data;
8+
9+
protected function addTemplateArguments(\Twig_Compiler $compiler)
10+
{
11+
if (null === $this->getNode('variables')) {
12+
if (false === $this->getAttribute('only')) {
13+
$compiler
14+
->raw('array_merge($context, ')
15+
->repr($this->data)
16+
->raw(')')
17+
;
18+
}
19+
else {
20+
$compiler->raw('array()');
21+
}
22+
} elseif (false === $this->getAttribute('only')) {
23+
$compiler
24+
->raw('array_merge($context, ')
25+
->repr($this->data)
26+
->raw(', ')
27+
->subcompile($this->getNode('variables'))
28+
->raw(')')
29+
;
30+
} else {
31+
$compiler->subcompile($this->getNode('variables'));
32+
}
33+
}
34+
}

Diff for: src/PatternLab/PatternEngine/Twig/IncludeNodeVisitor.php renamed to src/PatternLab/PatternEngine/Twig/PatternDataNodeVisitor.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PatternLab\Data;
66

7-
class IncludeNodeVisitor extends \Twig_BaseNodeVisitor
7+
class PatternDataNodeVisitor extends \Twig_BaseNodeVisitor
88
{
99
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
1010
{
@@ -17,7 +17,12 @@ protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
1717
if ($node->hasNode('expr') && $node->getNode('expr')->hasAttribute('value')) {
1818
$patternStoreKey = $node->getNode('expr')->getAttribute('value');
1919
$data = Data::getPatternSpecificData($patternStoreKey);
20-
$dataNode = new PatternDataIncludeNode($node, $data);
20+
if ($node instanceof \Twig_Node_Embed) {
21+
$dataNode = new PatternDataEmbedNode($node, $data);
22+
}
23+
else {
24+
$dataNode = new PatternDataIncludeNode($node, $data);
25+
}
2126

2227
return $dataNode;
2328
}

0 commit comments

Comments
 (0)