-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathStringLoader.php
88 lines (71 loc) · 2.35 KB
/
StringLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/*!
* Twig Pattern Engine Loader Class - String
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Sets an instance of Twig to deal with rendering of strings
*
*/
namespace PatternLab\PatternEngine\Twig\Loaders;
use \PatternLab\Config;
use \PatternLab\Dispatcher;
use \PatternLab\PatternEngine\Loader;
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_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();
TwigUtil::loadFunctions();
TwigUtil::loadTags();
TwigUtil::loadTests();
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()) {
$template = $this->instance->createTemplate($options["string"]);
return $template->render($options["data"]);
}
}