Skip to content

Commit 28c8695

Browse files
committed
adding support for dynamically loading filters
1 parent 69c7815 commit 28c8695

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/PatternLab/PatternEngine/Twig/TwigUtil.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,45 @@ public static function loadDebug($instance) {
6363
}
6464

6565
/**
66+
* Load filters for the Twig PatternEngine
67+
* @param {Instance} an instance of the twig engine
68+
*
69+
* @return {Instance} an instance of the twig engine
70+
*/
71+
public static function loadFilters($instance) {
72+
73+
// load defaults
74+
$filterDir = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_twig-components/filters";
75+
$filterExt = Config::getOption("twigFilterExt");
76+
$filterExt = $filterExt ? $filterExt : "filter.twig";
77+
78+
if (is_dir($filterDir)) {
79+
80+
// loop through the filter dir...
81+
$finder = new Finder();
82+
$finder->files()->name("*\.".$filterExt)->in($filterDir);
83+
$finder->sortByName();
84+
foreach ($finder as $file) {
85+
86+
include($file->getPathname());
87+
88+
// $filter should be defined in the included file
89+
if (isset($filter)) {
90+
$instance->addFilter($filter);
91+
unset($filter);
92+
}
93+
94+
}
95+
96+
} else {
97+
98+
self::dirNotExist($filterDir);
99+
100+
}
101+
102+
return $instance;
103+
104+
}
66105
* Load macros for the Twig PatternEngine
67106
* @param {Instance} an instance of the twig engine
68107
* @param {String} description of the loader type for the error

0 commit comments

Comments
 (0)