-
Notifications
You must be signed in to change notification settings - Fork 14
/
twig.engine
173 lines (155 loc) · 5.16 KB
/
twig.engine
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/* Drupal implementation of the twig template engine.
*
* You *need* PHP 5.3.x or higher to use code.
*
* @version 7.0.1.3
* @author René Bakx
*
*/
/**
* Register the needed classes with the autoloader
*/
$autoloader = UniversalClassLoader::getInstance();
$autoloader->registerPrefixes(array(
'Twig_' => DRUPAL_ROOT . '/sites/all/libraries/twig/lib/',
'TFD_' => DRUPAL_ROOT . '/sites/all/libraries/twig-for-drupal/',
));
/**
* registers the .tpl.html extension for twig templates
* @return string
*/
function twig_extension()
{
return ".tpl.twig";
}
/**
* Implementation of hook_init()
*
* note get's called at rebuild registry!
*
* @param <object> $theme
*/
function twig_init($theme)
{
if (file_exists($file = dirname($theme->filename) . '/template.php')) {
require_once($file);
}
}
/**
* Implementation of hook_theme()
*
* Registers both twig and php_template functions and templates
* which is needed to perform the fallback to .tpl.php
*
* @link http://api.drupal.org/api/function/hook_theme/7
* @return <array>
*/
function twig_theme($existing, $type, $theme, $path)
{
$templates = drupal_find_theme_functions($existing, array('twig', $theme));
$templates += drupal_find_theme_templates($existing, twig_extension(), $path);
return $templates;
}
/**
* Implementation of hook ENGINE_render_template
*
* Checks if the twig template is available or else let drupal fallback to phptemplate
*
* @param <string> $template template filename
* @param <array> $variables variables to be assigned to template
* @return <string> rendered template
*/
function twig_render_template($template, $variables = array())
{
$content = '';
if (file_exists($template)) {
try {
$twig = twig_get_instance();
$template = $twig->loadTemplate($template);
$content = $template->render($variables);
} catch (Exception $e) {
$content = t('Twig error "!error"', array('!error' => $e->getMessage()));
}
} else {
$content = t('Template (!template) not found ', array('!template' => $template));
}
return $content;
}
/**
* Clears the entire template cache folder
* @return void
*/
function twig_clear_cache()
{
twig_get_instance()->flushCompilerCache();
watchdog('twig', 'All caches cleared');
}
/*
* Returns a singleton version of the twig template engine
* @return <object> Twig_Environment
*/
function twig_get_instance()
{
static $twig_engine;
if (!is_object($twig_engine)) {
global $theme_info;
$twigEnvironment = array();
$twigEnvironment['autoescape'] = false; // Automatically escape all output
$twigEnvironment['auto_reload'] = true; //Whether to reload the template if the original source changed.
$twigEnvironment['debug'] = false; // When set to `true`, the generated templates have a __toString() method
$cache = 'public://twig_cache' . '/' . $theme_info->name;
if ($cache) {
$twigEnvironment['cache'] = $cache;
}
$loader = new TFD_Loader_Filesystem();
$twig_engine = new TFD_Environment($loader, $twigEnvironment);
$twig_engine->addExtension(new TFD_Extension());
}
return $twig_engine;
}
/**
* Find templates in the enabled themes.
* TODO: Clean up this goo.
* @return array
*/
function twig_get_discovered_templates()
{
$implementations = &drupal_static(__FUNCTION__, array());
$extension = twig_extension();
// Collect paths to all sub-themes grouped by base themes. These will be
// used for filtering. This allows base themes to have sub-themes in its
// folder hierarchy without affecting the base themes template discovery.
$theme_paths = array();
foreach (list_themes() as $theme_info) {
if (!empty($theme_info->base_theme)) {
$theme_paths[$theme_info->base_theme][$theme_info->name] = dirname($theme_info->filename);
}
}
foreach ($theme_paths as $basetheme => $subthemes) {
foreach ($subthemes as $subtheme => $subtheme_path) {
if (isset($theme_paths[$subtheme])) {
$theme_paths[$basetheme] = array_merge($theme_paths[$basetheme], $theme_paths[$subtheme]);
}
}
}
global $theme;
$subtheme_paths = isset($theme_paths[$theme]) ? $theme_paths[$theme] : array();
// Escape the periods in the extension.
$regex = '/' . str_replace('.', '\.', $extension) . '$/';
$currentPath = drupal_get_path('theme', variable_get('theme_default', 'null'));
// Get a listing of all template files in the path to search.
$files = drupal_system_listing($regex, $currentPath, 'name', 0);
// Find templates that implement registered theme hooks and include that in
// what is returned so that the registry knows that the theme has this
// implementation.
foreach ($files as $template => $file) {
// Ignore sub-theme templates for the current theme.
if (strpos($file->uri, str_replace($subtheme_paths, '', $file->uri)) !== 0) {
continue;
}
$implementations[] = dirname($file->uri);
}
$implementations = array_unique($implementations);
return $implementations;
}