Skip to content

Commit 31b5701

Browse files
committed
adding support for dynamically loading functions
1 parent 28c8695 commit 31b5701

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/PatternLab/PatternEngine/Twig/TwigUtil.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,49 @@ public static function loadFilters($instance) {
102102
return $instance;
103103

104104
}
105+
106+
/**
107+
* Load functions for the Twig PatternEngine
108+
* @param {Instance} an instance of the twig engine
109+
*
110+
* @return {Instance} an instance of the twig engine
111+
*/
112+
public static function loadFunctions($instance) {
113+
114+
// load defaults
115+
$functionDir = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_twig-components/functions";
116+
$functionExt = Config::getOption("twigFunctionExt");
117+
$functionExt = $functionExt ? $functionExt : "function.twig";
118+
119+
if (is_dir($functionDir)) {
120+
121+
// loop through the function dir...
122+
$finder = new Finder();
123+
$finder->files()->name("*\.".$functionExt)->in($functionDir);
124+
$finder->sortByName();
125+
foreach ($finder as $file) {
126+
127+
include($file->getPathname());
128+
129+
// $function should be defined in the included file
130+
if (isset($function)) {
131+
$instance->addFunction($function);
132+
unset($function);
133+
}
134+
135+
}
136+
137+
} else {
138+
139+
self::dirNotExist($functionDir);
140+
141+
}
142+
143+
return $instance;
144+
145+
}
146+
147+
/**
105148
* Load macros for the Twig PatternEngine
106149
* @param {Instance} an instance of the twig engine
107150
* @param {String} description of the loader type for the error

0 commit comments

Comments
 (0)