Skip to content

Commit 3c15d98

Browse files
committed
Merge branch 'master' into dev
2 parents f90a953 + 26c4a93 commit 3c15d98

File tree

6 files changed

+83
-36
lines changed

6 files changed

+83
-36
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ Would be used like this in a pattern:
7676

7777
### Template inheritance
7878

79-
The requirements for using template inheritance with Pattern Lab:
79+
How to use [Template Inheritance](http://twig.sensiolabs.org/doc/templates.html#template-inheritance) with Pattern Lab:
8080

81-
* Files must go in `source/_layouts`
82-
* Files must have the extension `.twig`
83-
* The filename will be used as the reference in the `extends` tag
81+
* Files must have the extension `.twig`.
82+
* Files can be extended either by using Pattern Lab's normal shorthand syntax (e.g, `{% extends 'templates-extended-layout'%}`).
83+
* Files can optionally go in `source/_layouts` in order to hide them from the list of patterns and then you can just use the filename as reference (e.g., `{% extends 'extended-layout'%}`).
84+
* Files that are in the same directory can also just use the file name without the shorthand syntax (however, it must include the extension). So if `file1.twig` and `file2.twig` were in same directory, you could place this code in `file2.twig`: `{% extends 'file1.twig' %}`.
8485

8586
An example of a simple layout called `base.twig` in `source/_layouts`:
8687

@@ -124,6 +125,8 @@ Would be used like this in a pattern:
124125
{% endblock %}
125126
```
126127

128+
All uses of `extends` above also work with `includes`, `embed` and most likely many other Twig Tags. Let us know if you run into interesting or unexpected use cases!
129+
127130
## Extending Twig Further
128131

129132
Twig comes with a number of ways to extend the underlying template parser. You can you can add [extra tags](http://twig.sensiolabs.org/doc/advanced.html#tags), [filters](http://twig.sensiolabs.org/doc/advanced.html#filters), [tests](http://twig.sensiolabs.org/doc/advanced.html#tests), and [functions](http://twig.sensiolabs.org/doc/advanced.html#functions). The Twig PatternEngine tries to simplify these extensions by allowing you to create files in specific folders and then auto-load the extensions for you. Learn more about:

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

Lines changed: 2 additions & 2 deletions
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
/**
Lines changed: 23 additions & 0 deletions
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+
}

src/PatternLab/PatternEngine/Twig/PatternDataIncludeNode.php

Lines changed: 10 additions & 28 deletions
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
}
Lines changed: 34 additions & 0 deletions
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+
}

src/PatternLab/PatternEngine/Twig/IncludeNodeVisitor.php renamed to src/PatternLab/PatternEngine/Twig/PatternDataNodeVisitor.php

Lines changed: 7 additions & 2 deletions
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)