Skip to content

Add "Globals" Twig Extension Support #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function __construct($options = array()) {
TwigUtil::setInstance($instance);
TwigUtil::loadFilters();
TwigUtil::loadFunctions();
TwigUtil::loadGlobals();
TwigUtil::loadTags();
TwigUtil::loadTests();
TwigUtil::loadDateFormats();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function __construct($options = array()) {
TwigUtil::setInstance($instance);
TwigUtil::loadFilters();
TwigUtil::loadFunctions();
TwigUtil::loadGlobals();
TwigUtil::loadTags();
TwigUtil::loadTests();
TwigUtil::loadDateFormats();
Expand Down
1 change: 1 addition & 0 deletions src/PatternLab/PatternEngine/Twig/Loaders/StringLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function __construct($options = array()) {
TwigUtil::setInstance($instance);
TwigUtil::loadFilters();
TwigUtil::loadFunctions();
TwigUtil::loadGlobals();
TwigUtil::loadTags();
TwigUtil::loadTests();
TwigUtil::loadDateFormats();
Expand Down
46 changes: 45 additions & 1 deletion src/PatternLab/PatternEngine/Twig/TwigUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,51 @@ public static function loadMacros() {
}

}



/**
* Load Twig Globals for the Twig PatternEngine
*/
public static function loadGlobals() {

// load defaults
$globalDir = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_twig-components/globals";
$globalExt = Config::getOption("twigGlobalExt");
$globalExt = $globalExt ? $globalExt : "global.php";

if (is_dir($globalDir)) {

// loop through the global dir...
$finder = new Finder();
$finder->files()->name("*\.".$globalExt)->in($globalDir);
$finder->sortByName();
foreach ($finder as $file) {

// see if the file should be ignored or not
$baseName = $file->getBasename();
if ($baseName[0] != "_") {

include_once($file->getPathname());

$twigExtensionName = 'Twig_Extension_' . $file->getBasename('.' . $globalExt);
$twigExtensionName = str_replace('-', '_', $twigExtensionName); //Replace dashes with underscores for Class name

$extension = new $twigExtensionName('xyz');
$globals = $extension->getGlobals();

if (isset($globals)) {
// If global Twig values exist, loop through and add to Pattern Lab
foreach ($globals as $name => $value) {
self::$instance->addGlobal($name, $value);
}
unset($globals);
}
}
}
}
}


/**
* Load tags for the Twig PatternEngine
*/
Expand Down