Skip to content

Commit cf208ac

Browse files
committed
Merge branch 'include-pattern-specific-data' of https://github.com/aleksip/patternengine-php-twig into hotfix-prs
2 parents 49038ad + b5ed124 commit cf208ac

File tree

5 files changed

+85
-9
lines changed

5 files changed

+85
-9
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ The requirements for using filters with Pattern Lab:
148148
* The filter **must** set the variable `$filter`
149149
* Only one filter per file (_e.g. can only set `$filter` once per file_)
150150

151-
An example function called `rot13.filter.twig` in `source/_twig-components/filters`:
151+
An example function called `rot13.filter.php` in `source/_twig-components/filters`:
152152

153153
```php
154154
<?php
@@ -175,7 +175,7 @@ The requirements for using functions with Pattern Lab:
175175
* The function **must** set the variable `$function`
176176
* Only one function per file (_e.g. can only set `$function` once per file_)
177177

178-
An example function called `boo.function.twig` in `source/_twig-components/functions`:
178+
An example function called `boo.function.php` in `source/_twig-components/functions`:
179179

180180
```php
181181
<?php
@@ -202,7 +202,7 @@ The requirements for using tests with Pattern Lab:
202202
* The test **must** set the variable `$test`
203203
* Only one test per file (_e.g. can only set `$test` once per file_)
204204

205-
An example of a simple test called `red.test.twig` in `source/_twig-components/tests`:
205+
An example of a simple test called `red.test.php` in `source/_twig-components/tests`:
206206

207207
```php
208208
<?php
@@ -254,7 +254,7 @@ Tags are the most complicated extension to set-up with Pattern Lab. Three steps
254254

255255
Pattern Lab takes care of the registering for you based on the file name.
256256

257-
An example of a simple tag called `setdupe.tag.twig` in `source/_twig-components/tags` that mimics the default `set` tag. Please note all of the locations where class names incorporate the filename, `setdupe`.
257+
An example of a simple tag called `setdupe.tag.php` in `source/_twig-components/tags` that mimics the default `set` tag. Please note all of the locations where class names incorporate the filename, `setdupe`.
258258

259259
```php
260260
<?php
@@ -328,11 +328,11 @@ You can modify the default date and interval formats for Twig by editing the `tw
328328

329329
To disable extensions that you're no longer using simply add an underscore to the beginning of a filename and then re-generate your site. For example, the enabled rot13 filter:
330330

331-
source/_twig-components/filters/rot13.filter.twig
331+
source/_twig-components/filters/rot13.filter.php
332332

333333
And the disabled rot13 filter:
334334

335-
source/_twig-components/filters/_rot13.filter.twig
335+
source/_twig-components/filters/_rot13.filter.php
336336

337337
Then re-generate your Pattern Lab site with:
338338

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
},
2626
"require": {
2727
"php": ">=5.3.6",
28-
"twig/twig": "~1.15",
29-
"pattern-lab/core": "dev-dev"
28+
"pattern-lab/core": "0.*",
29+
"twig/twig": "1.*"
3030
},
3131
"extra": {
3232
"patternlab": {
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)