Skip to content

Proof-of-concept Twig 2.x integration #43

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 2 commits 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"require": {
"pattern-lab/core": "^2.0.0",
"twig/twig": "~1.0"
"twig/twig": "^2.0.0"
},
"extra": {
"patternlab": {
Expand Down
26 changes: 13 additions & 13 deletions src/PatternLab/PatternEngine/Twig/Loaders/FilesystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,36 @@
use \PatternLab\PatternEngine\Twig\TwigUtil;

class FilesystemLoader extends Loader {

/**
* Load a new Twig instance that uses the File System Loader
*/
public function __construct($options = array()) {

// set-up default vars
$twigDebug = Config::getOption("twigDebug");

// set-up the paths to be searched for templates
$filesystemLoaderPaths = array();
$filesystemLoaderPaths[] = $options["templatePath"];
$filesystemLoaderPaths[] = $options["partialsPath"];

// see if source/_macros exists. if so add it to be searchable
$macrosPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
if (is_dir($macrosPath)) {
$filesystemLoaderPaths[] = $macrosPath;
}

// see if source/_layouts exists. if so add it to be searchable
$layoutsPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_layouts";
if (is_dir($layoutsPath)) {
$filesystemLoaderPaths[] = $layoutsPath;
}

// set-up Twig
$twigLoader = new \Twig_Loader_Filesystem($filesystemLoaderPaths);
$instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug));

// customize Twig
TwigUtil::setInstance($instance);
TwigUtil::loadFilters();
Expand All @@ -57,27 +57,27 @@ public function __construct($options = array()) {
TwigUtil::loadDateFormats();
TwigUtil::loadDebug();
TwigUtil::loadMacros();

// set-up the dispatcher
$dispatcherInstance = Dispatcher::getInstance();
$dispatcherInstance->dispatch("twigLoader.customize");
$dispatcherInstance->dispatch("twigFilesystemLoader.customize");

// get the instance
$this->instance = TwigUtil::getInstance();

}

/**
* Render a template
* @param {Array} the options to be rendered by Twig
*
* @return {String} the rendered result
*/
public function render($options = array()) {

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

}

}
39 changes: 11 additions & 28 deletions src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@
use \PatternLab\PatternEngine\Twig\TwigUtil;

class PatternLoader extends Loader {

/**
* Load a new Twig instance that uses the Pattern Loader
*/
public function __construct($options = array()) {

// set-up default vars
$twigDebug = Config::getOption("twigDebug");
$twigAutoescape = Config::getOption("twigAutoescape");

// go through various places where things can exist
$filesystemLoaderPaths = array();

// see if source/_macros exists
$macrosPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
if (is_dir($macrosPath)) {
$filesystemLoaderPaths[] = $macrosPath;
}

// see if source/_layouts exists. if so add it to be searchable
$layoutsPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_layouts";
if (is_dir($layoutsPath)) {
Expand All @@ -60,7 +60,7 @@ public function __construct($options = array()) {
$filesystemLoaderPaths[] = $object->getPathname();
}
}

// set-up the loader list in order that they should be checked
// 1. Patterns 2. Filesystem 3. String
$loaders = array();
Expand All @@ -81,15 +81,10 @@ public function __construct($options = array()) {
// getting the loaders back
$loaders = TwigUtil::getLoaders();

// 3. add String loader
// This *must* go last or no loaders after will work ~ https://github.com/symfony/symfony/issues/10865
// @todo Remove `Twig_Loader_String` - if a Twig include path is wrong, this outputs the string anyway with no error ~ https://github.com/symfony/symfony/issues/10865
$loaders[] = new \Twig_Loader_String();

// set-up Twig
$twigLoader = new \Twig_Loader_Chain($loaders);
$instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug, "autoescape" => $twigAutoescape));

// customize Twig
TwigUtil::setInstance($instance);
TwigUtil::loadFilters();
Expand All @@ -105,29 +100,17 @@ public function __construct($options = array()) {

// get the instance
$this->instance = TwigUtil::getInstance();

}

/**
* Render a pattern
* @param {Array} the options to be rendered by Twig
*
* @return {String} the rendered result
*/
public function render($options = array()) {

$result = $this->instance->render($options["pattern"], $options["data"]);
// This error handler catches files that didn't render using any of the loaders.
// The most common scenario is when a file's contents get passed to and through `Twig_Loader_String` and
// outputs the raw Twig file contents like `@atoms/buttons/button.twig`.
// @todo Remove this once `Twig_Loader_String` is removed.
if (strpos($result, "@") === 0) {
echo "Twig file not found: " . $result . "\n";
exit(1);
} else {
return $result;
}

$template = $this->instance->createTemplate($options["pattern"]);
return $template->render($options["data"]);
}

}
31 changes: 15 additions & 16 deletions src/PatternLab/PatternEngine/Twig/Loaders/StringLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,42 @@
use \PatternLab\PatternEngine\Twig\TwigUtil;

class StringLoader extends Loader {

/**
* Load a new Twig instance that is just a vanilla Twig rendering engine for strings
*/
public function __construct($options = array()) {

// set-up the defaults
$twigDebug = Config::getOption("twigDebug");

// go through various places where things can exist
$filesystemLoaderPaths = array();

// see if source/_macros exists
$macrosPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
if (is_dir($macrosPath)) {
$filesystemLoaderPaths[] = $macrosPath;
}

// see if source/_layouts exists. if so add it to be searchable
$layoutsPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_layouts";
if (is_dir($layoutsPath)) {
$filesystemLoaderPaths[] = $layoutsPath;
}

// set-up the loader list
$loaders = array();
// add the paths to the filesystem loader if the paths existed
if (count($filesystemLoaderPaths) > 0) {
$loaders[] = new \Twig_Loader_Filesystem($filesystemLoaderPaths);
}
$loaders[] = new \Twig_Loader_String();
$loaders[] = new \Twig_Loader_Array();

// set-up Twig
$twigLoader = new \Twig_Loader_Chain($loaders);
$instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug));

// customize Twig
TwigUtil::setInstance($instance);
TwigUtil::loadFilters();
Expand All @@ -63,27 +63,26 @@ public function __construct($options = array()) {
TwigUtil::loadDateFormats();
TwigUtil::loadDebug();
TwigUtil::loadMacros();

// set-up the dispatcher
$dispatcherInstance = Dispatcher::getInstance();
$dispatcherInstance->dispatch("twigLoader.customize");
$dispatcherInstance->dispatch("twigStringLoader.customize");

// get the instance
$this->instance = TwigUtil::getInstance();

}

/**
* Render a string
* @param {Array} the options to be rendered by Twig
*
* @return {String} the rendered result
*/
public function render($options = array()) {

return $this->instance->render($options["string"], $options["data"]);

$template = $this->instance->createTemplate($options["string"]);
return $template->render($options["data"]);
}

}
Loading