Skip to content

Commit 6112e1a

Browse files
committed
Merge branch 'dev'
2 parents 888d69c + 761c8ac commit 6112e1a

File tree

7 files changed

+163
-18
lines changed

7 files changed

+163
-18
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ Pattern Lab PHP uses [Composer](https://getcomposer.org/) to manage project depe
88

99
composer require pattern-lab/patternengine-twig
1010

11+
## Using Twig Macros
12+
13+
The Twig PatternEngine will automatically find and load Twig macros making them available to every pattern. To use this feature add your `.macro` files to `source/_macros/`. If your macro file is called `forms.macro` and it has a macro called `input()` you'd access it in your Twig templates as `{{ forms.input() }}`. **Please note:** ensure that there is no overlap between the keys for your macros and the keys for your data attributes.
14+
15+
## Enabling `dump()`
16+
17+
To use `dump()` set `twigDebug` in `configs/config.yml` to `true`.
18+
19+
## Modifying the Default Date and Interval Format
20+
21+
You can modify the default date and interval formats for Twig by editing the `twigDefaultDateFormat` and `twigDefaultIntervalFormat` in `configs/config.yml`. Set them to an empty string to use Twig's default formats. **Please note:** both must be set for this feature to work.
22+
1123
## Available Loaders
1224

1325
If you're building a plugin that will be parsing Twig files you have access to three loaders. It's recommended that you use these instead of accessing Twig directly as these loaders will work with other PatternEngines.
@@ -41,9 +53,9 @@ The pattern loader looks for patterns and allows the use of the Pattern Lab-spec
4153

4254
```php
4355
$data = array(...);
44-
$patternPath = "path/to/pattern";
56+
$patternContent = file_get_contents("path/to/pattern");
4557
$patternEngineBasePath = \PatternLab\PatternEngine::getInstance()->getBasePath();
4658
$patternLoaderClass = $patternEngineBasePath."\Loaders\PatternLoader";
4759
$patternLoader = new $patternLoaderClass($options);
48-
$code = $patternLoader->render(array("pattern" => $patternPath, "data" => $data));
60+
$code = $patternLoader->render(array("pattern" => $patternContent, "data" => $data));
4961
print $output; // outputs the given pattern

composer.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pattern-lab/patternengine-twig",
3-
"description": "Twig-based Pattern Engine for the PHP version of Pattern Lab",
3+
"description": "Twig-based PatternEngine for Pattern Lab.",
44
"keywords": ["twig", "pattern lab", "pattern engine"],
55
"homepage": "http://patternlab.io",
66
"license": "MIT",
@@ -25,12 +25,18 @@
2525
},
2626
"require": {
2727
"php": ">=5.3.6",
28-
"twig/twig": "v1.15.1"
28+
"twig/twig": "~1.15"
2929
},
3030
"extra": {
3131
"patternlab": {
3232
"config": [
33-
{ "patternExtension": "twig" }
33+
{
34+
"patternExtension": "twig",
35+
"twigDebug": false,
36+
"twigDefaultDateFormat": "",
37+
"twigDefaultIntervalFormat": "",
38+
"twigMacroExt": "macro"
39+
}
3440
]
3541
}
3642
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
namespace PatternLab\PatternEngine\Twig\Loaders;
1414

15+
use \PatternLab\Config;
1516
use \PatternLab\PatternEngine\Loader;
17+
use \PatternLab\PatternEngine\Twig\TwigUtil;
1618

1719
class FilesystemLoader extends Loader {
1820

@@ -21,8 +23,16 @@ class FilesystemLoader extends Loader {
2123
*/
2224
public function __construct($options = array()) {
2325

24-
$twigLoader = new \Twig_Loader_Filesystem(array($options["templatePath"],$options["partialsPath"]));
25-
$this->instance = new \Twig_Environment($twigLoader);
26+
// set-up the loader
27+
$twigDebug = Config::getOption("twigDebug");
28+
$macroPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
29+
$twigLoader = new \Twig_Loader_Filesystem(array($options["templatePath"],$options["partialsPath"],$macroPath));
30+
$this->instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug));
31+
32+
// customize the loader
33+
$this->instance = TwigUtil::loadDateFormats($this->instance);
34+
$this->instance = TwigUtil::loadDebug($this->instance);
35+
$this->instance = TwigUtil::loadMacros($this->instance, "filesystem");
2636

2737
}
2838

@@ -34,7 +44,7 @@ public function __construct($options = array()) {
3444
*/
3545
public function render($options = array()) {
3646

37-
return $this->instance->render($options["template"].".twig", $options["data"]);
47+
return $this->instance->render($options["template"].".".Config::getOption("patternExtension"), $options["data"]);
3848

3949
}
4050

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
<?php
22

33
/*!
4-
* Mustache Pattern Engine Loader Class - Patterns
4+
* Twig Pattern Engine Loader Class - Patterns
55
*
66
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
77
* Licensed under the MIT license
88
*
9-
* Sets an instance of Mustache to deal with rendering of patterns
9+
* Sets an instance of Twig to deal with patterns. Tries to find
10+
* files on system first. If not tries to load them as strings.
1011
*
1112
*/
1213

1314
namespace PatternLab\PatternEngine\Twig\Loaders;
1415

1516
use \PatternLab\Config;
16-
use \PatternLab\PatternEngine\Twig\Loaders\Twig\PatternLoader as Twig_Loader_PatternLoader;
17+
use \PatternLab\PatternEngine\Twig\Loaders\Twig\PatternPartialLoader as Twig_Loader_PatternPartialLoader;
18+
use \PatternLab\PatternEngine\Twig\Loaders\Twig\PatternStringLoader as Twig_Loader_PatternStringLoader;
1719
use \PatternLab\PatternEngine\Loader;
20+
use \PatternLab\PatternEngine\Twig\TwigUtil;
1821

1922
class PatternLoader extends Loader {
2023

@@ -23,10 +26,20 @@ class PatternLoader extends Loader {
2326
*/
2427
public function __construct($options = array()) {
2528

26-
//default var
27-
$patternSourceDir = Config::getOption("patternSourceDir");
28-
$twigLoader = new Twig_Loader_PatternLoader($patternSourceDir,array("patternPaths" => $options["patternPaths"]));
29-
$this->instance = new \Twig_Environment($twigLoader);
29+
// set-up the loader
30+
$twigDebug = Config::getOption("twigDebug");
31+
$patternSourceDir = Config::getOption("patternSourceDir");
32+
$macroPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
33+
$patternPartialLoader = new Twig_Loader_PatternPartialLoader($patternSourceDir,array("patternPaths" => $options["patternPaths"]));
34+
$patternStringLoader = new \Twig_Loader_String();
35+
$macroLoader = new \Twig_Loader_Filesystem(array($macroPath));
36+
$twigLoader = new \Twig_Loader_Chain(array($patternPartialLoader, $macroLoader, $patternStringLoader));
37+
$this->instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug));
38+
39+
// customize the loader
40+
$this->instance = TwigUtil::loadDateFormats($this->instance);
41+
$this->instance = TwigUtil::loadDebug($this->instance);
42+
$this->instance = TwigUtil::loadMacros($this->instance, "pattern");
3043

3144
}
3245

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
namespace PatternLab\PatternEngine\Twig\Loaders;
1414

15+
use \PatternLab\Config;
1516
use \PatternLab\PatternEngine\Loader;
17+
use \PatternLab\PatternEngine\Twig\TwigUtil;
1618

1719
class StringLoader extends Loader {
1820

@@ -21,8 +23,18 @@ class StringLoader extends Loader {
2123
*/
2224
public function __construct($options = array()) {
2325

24-
$twigLoader = new \Twig_Loader_String();
25-
$this->instance = new \Twig_Environment($twigLoader);
26+
// set-up the loader
27+
$twigDebug = Config::getOption("twigDebug");
28+
$macroPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
29+
$macroLoader = new \Twig_Loader_Filesystem(array($macroPath));
30+
$stringLoader = new \Twig_Loader_String();
31+
$twigLoader = new \Twig_Loader_Chain(array($macroLoader, $stringLoader));
32+
$this->instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug));
33+
34+
// customize the loader
35+
$this->instance = TwigUtil::loadDateFormats($this->instance);
36+
$this->instance = TwigUtil::loadDebug($this->instance);
37+
$this->instance = TwigUtil::loadMacros($this->instance, "string");
2638

2739
}
2840

src/PatternLab/PatternEngine/Twig/Loaders/Twig/PatternLoader.php renamed to src/PatternLab/PatternEngine/Twig/Loaders/Twig/PatternPartialLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use \PatternLab\PatternEngine\Util;
1717

18-
class PatternLoader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface {
18+
class PatternPartialLoader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface {
1919

2020
/** Identifier of the main namespace. */
2121
const MAIN_NAMESPACE = '__main__';
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*!
4+
* Twig Pattern Engine Loader Class - String
5+
*
6+
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
7+
* Licensed under the MIT license
8+
*
9+
* Sets an instance of Twig to deal with rendering of strings
10+
*
11+
*/
12+
13+
namespace PatternLab\PatternEngine\Twig;
14+
15+
use \PatternLab\Config;
16+
use \PatternLab\Console;
17+
use \Symfony\Component\Finder\Finder;
18+
19+
class TwigUtil {
20+
21+
/**
22+
* Load custom date formats for Twig
23+
* @param {Instance} an instance of the twig engine
24+
*
25+
* @return {Instance} an instance of the twig engine
26+
*/
27+
public static function loadDateFormats($instance) {
28+
29+
$dateFormat = Config::getOption("twigDefaultDateFormat");
30+
$intervalFormat = Config::getOption("twigDefaultIntervalFormat");
31+
32+
if ($dateFormat && $intervalFormat && !empty($dateFormat) && !empty($intervalFormat)) {
33+
$instance->getExtension("core")->setDateFormat($dateFormat, $intervalFormat);
34+
}
35+
36+
return $instance;
37+
38+
}
39+
40+
/**
41+
* Enable the debug options for Twig
42+
* @param {Instance} an instance of the twig engine
43+
*
44+
* @return {Instance} an instance of the twig engine
45+
*/
46+
public static function loadDebug($instance) {
47+
48+
if (Config::getOption("twigDebug")) {
49+
$instance->addExtension(new \Twig_Extension_Debug());
50+
}
51+
52+
return $instance;
53+
54+
}
55+
56+
/**
57+
* Load macros for the Twig PatternEngine
58+
* @param {Instance} an instance of the twig engine
59+
* @param {String} description of the loader type for the error
60+
*
61+
* @return {Instance} an instance of the twig engine
62+
*/
63+
public static function loadMacros($instance, $instanceType) {
64+
65+
// load defaults
66+
$macroDir = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
67+
$macroExt = Config::getOption("twigMacroExt");
68+
$macroExt = $macroExt ? $macroExt : "macro";
69+
70+
if (is_dir($macroDir)) {
71+
72+
// loop through some macro containing dir and run...
73+
$finder = new Finder();
74+
$finder->files()->name("*.".$macroExt)->in($macroDir);
75+
$finder->sortByName();
76+
foreach ($finder as $file) {
77+
$instance->addGlobal($file->getBasename(".".$macroExt), $instance->loadTemplate($file->getBasename()));
78+
}
79+
80+
} else {
81+
82+
// write warning because the macro dir doesn't exist
83+
$macroDirHR = Console::getHumanReadablePath($macroDir);
84+
Console::writeWarning("the path <path>".$macroDirHR."</path> doesn't exist so macros won't be loaded for the ".$instanceType." loader...");
85+
86+
}
87+
88+
return $instance;
89+
90+
}
91+
92+
}

0 commit comments

Comments
 (0)