Skip to content

Commit 6fe07d6

Browse files
committed
adding support for filters, functions, tags, tests, macros, & layouts
1 parent 8cde6c2 commit 6fe07d6

File tree

3 files changed

+94
-20
lines changed

3 files changed

+94
-20
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,38 @@ class FilesystemLoader extends Loader {
2323
*/
2424
public function __construct($options = array()) {
2525

26-
// set-up the loader
26+
// set-up default vars
2727
$twigDebug = Config::getOption("twigDebug");
28-
$macroPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
29-
$twigLoader = new \Twig_Loader_Filesystem(array($options["templatePath"],$options["partialsPath"],$macroPath));
28+
29+
// set-up the paths to be searched for templates
30+
$dirPaths = array();
31+
$dirPaths[] = $options["templatePath"];
32+
$dirPaths[] = $options["partialsPath"];
33+
34+
// see if source/_macros exists. if so add it to be searchable
35+
$macrosPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
36+
if (is_dir($macrosPath)) {
37+
$dirPaths[] = $macrosPath;
38+
}
39+
40+
// see if source/_layouts exists. if so add it to be searchable
41+
$layoutsPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_layouts";
42+
if (is_dir($layoutsPath)) {
43+
$dirPaths[] = $layoutsPath;
44+
}
45+
46+
// set-up Twig
47+
$twigLoader = new \Twig_Loader_Filesystem($dirPaths);
3048
$this->instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug));
3149

32-
// customize the loader
50+
// customize Twig
51+
$this->instance = TwigUtil::loadFilters($this->instance);
52+
$this->instance = TwigUtil::loadFunctions($this->instance);
53+
$this->instance = TwigUtil::loadTags($this->instance);
54+
$this->instance = TwigUtil::loadTests($this->instance);
3355
$this->instance = TwigUtil::loadDateFormats($this->instance);
3456
$this->instance = TwigUtil::loadDebug($this->instance);
35-
$this->instance = TwigUtil::loadMacros($this->instance, "filesystem");
57+
$this->instance = TwigUtil::loadMacros($this->instance);
3658

3759
}
3860

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

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,46 @@ class PatternLoader extends Loader {
2626
*/
2727
public function __construct($options = array()) {
2828

29-
// set-up the loader
29+
// set-up default vars
3030
$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));
31+
32+
// set-up the loader list
33+
$loaders = array();
34+
$filesystemLoaderPaths = array();
35+
$loaders[] = new Twig_Loader_PatternPartialLoader(Config::getOption("patternSourceDir"),array("patternPaths" => $options["patternPaths"]));
36+
37+
38+
// see if source/_macros exists
39+
$macrosPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
40+
if (is_dir($macrosPath)) {
41+
$filesystemLoaderPaths[] = $macrosPath;
42+
}
43+
44+
// see if source/_layouts exists. if so add it to be searchable
45+
$layoutsPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_layouts";
46+
if (is_dir($layoutsPath)) {
47+
$filesystemLoaderPaths[] = $layoutsPath;
48+
}
49+
50+
// add the paths to the filesystem loader if the paths existed
51+
if (count($filesystemLoaderPaths) > 0) {
52+
$loaders[] = new \Twig_Loader_Filesystem($filesystemLoaderPaths);
53+
}
54+
55+
$loaders[] = new \Twig_Loader_String();
56+
57+
// set-up Twig
58+
$twigLoader = new \Twig_Loader_Chain($loaders);
3759
$this->instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug));
3860

39-
// customize the loader
61+
// customize Twig
62+
$this->instance = TwigUtil::loadFilters($this->instance);
63+
$this->instance = TwigUtil::loadFunctions($this->instance);
64+
$this->instance = TwigUtil::loadTags($this->instance);
65+
$this->instance = TwigUtil::loadTests($this->instance);
4066
$this->instance = TwigUtil::loadDateFormats($this->instance);
4167
$this->instance = TwigUtil::loadDebug($this->instance);
42-
$this->instance = TwigUtil::loadMacros($this->instance, "pattern");
68+
$this->instance = TwigUtil::loadMacros($this->instance);
4369

4470
}
4571

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,44 @@ class StringLoader extends Loader {
2323
*/
2424
public function __construct($options = array()) {
2525

26-
// set-up the loader
26+
// set-up the defaults
2727
$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));
28+
29+
// set-up the loader list
30+
$loaders = array();
31+
$filesystemLoaderPaths = array();
32+
33+
// see if source/_macros exists
34+
$macrosPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
35+
if (is_dir($macrosPath)) {
36+
$filesystemLoaderPaths[] = $macrosPath;
37+
}
38+
39+
// see if source/_layouts exists. if so add it to be searchable
40+
$layoutsPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_layouts";
41+
if (is_dir($layoutsPath)) {
42+
$filesystemLoaderPaths[] = $layoutsPath;
43+
}
44+
45+
// add the paths to the filesystem loader if the paths existed
46+
if (count($filesystemLoaderPaths) > 0) {
47+
$loaders[] = new \Twig_Loader_Filesystem($filesystemLoaderPaths);
48+
}
49+
50+
$loaders[] = new \Twig_Loader_String();
51+
52+
// set-up Twig
53+
$twigLoader = new \Twig_Loader_Chain($loaders);
3254
$this->instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug));
3355

3456
// customize the loader
57+
$this->instance = TwigUtil::loadFilters($this->instance);
58+
$this->instance = TwigUtil::loadFunctions($this->instance);
59+
$this->instance = TwigUtil::loadTags($this->instance);
60+
$this->instance = TwigUtil::loadTests($this->instance);
3561
$this->instance = TwigUtil::loadDateFormats($this->instance);
3662
$this->instance = TwigUtil::loadDebug($this->instance);
37-
$this->instance = TwigUtil::loadMacros($this->instance, "string");
63+
$this->instance = TwigUtil::loadMacros($this->instance);
3864

3965
}
4066

0 commit comments

Comments
 (0)