Skip to content

Commit b5ed124

Browse files
committed
Include pattern specific data
1 parent 3e28821 commit b5ed124

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace PatternLab\PatternEngine\Twig;
4+
5+
use PatternLab\Data;
6+
7+
class IncludeNodeVisitor extends \Twig_BaseNodeVisitor
8+
{
9+
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
10+
{
11+
return $node;
12+
}
13+
14+
protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
15+
{
16+
if ($node instanceof \Twig_Node_Include) {
17+
if ($node->hasNode('expr') && $node->getNode('expr')->hasAttribute('value')) {
18+
$patternStoreKey = $node->getNode('expr')->getAttribute('value');
19+
$data = Data::getPatternSpecificData($patternStoreKey);
20+
$dataNode = new PatternDataIncludeNode($node, $data);
21+
22+
return $dataNode;
23+
}
24+
}
25+
26+
return $node;
27+
}
28+
29+
public function getPriority()
30+
{
31+
return 0;
32+
}
33+
}

src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace PatternLab\PatternEngine\Twig\Loaders;
1515

1616
use \PatternLab\Config;
17+
use \PatternLab\PatternEngine\Twig\IncludeNodeVisitor;
1718
use \PatternLab\PatternEngine\Twig\Loaders\Twig\PatternPartialLoader as Twig_Loader_PatternPartialLoader;
1819
use \PatternLab\PatternEngine\Twig\Loaders\Twig\PatternStringLoader as Twig_Loader_PatternStringLoader;
1920
use \PatternLab\PatternEngine\Loader;
@@ -66,7 +67,9 @@ public function __construct($options = array()) {
6667
$this->instance = TwigUtil::loadDateFormats($this->instance);
6768
$this->instance = TwigUtil::loadDebug($this->instance);
6869
$this->instance = TwigUtil::loadMacros($this->instance);
69-
70+
71+
// add node visitor
72+
$this->instance->addNodeVisitor(new IncludeNodeVisitor());
7073
}
7174

7275
/**
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace PatternLab\PatternEngine\Twig;
4+
5+
class PatternDataIncludeNode extends \Twig_Node_Include
6+
{
7+
protected $data;
8+
9+
public function __construct(\Twig_Node_Include $originalNode, $data)
10+
{
11+
\Twig_Node::__construct($originalNode->nodes, $originalNode->attributes, $originalNode->lineno, $originalNode->tag);
12+
$this->data = $data;
13+
}
14+
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+
}
39+
}
40+
}

0 commit comments

Comments
 (0)