/[drupal]/contributions/theme-engines/smarty/smarty.engine
ViewVC logotype

Contents of /contributions/theme-engines/smarty/smarty.engine

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.16 - (show annotations) (download) (as text)
Sat Oct 20 04:15:24 2007 UTC (2 years, 1 month ago) by djnz
Branch: MAIN
CVS Tags: DRUPAL-6--2-0, HEAD
Changes since 1.15: +39 -27 lines
File MIME type: text/x-php
Improve refactoring to work with as much built-in Drupal 6 code as possible.
1 <?php
2 // $Id: phptemplate.engine,v 1.69 2007/10/02 16:19:23 dries Exp $
3
4 /**
5 * @file
6 * Handles integration of templates written for the Smarty template engine
7 * (http://www.smarty.net) with the Drupal theme system.
8 */
9
10 // check smarty is going to work to avoid a blank screen if it is activated
11 _smarty_init();
12
13 function smarty_init($theme) {
14 _smarty_init();
15 $file = dirname($theme->filename) .'/template.php';
16 if (file_exists($file)) {
17 include_once "./$file";
18 }
19 }
20
21 /**
22 * Return the file extension.
23 *
24 * @return
25 * The extension
26 */
27 function smarty_extension() {
28 return '.tpl';
29 }
30
31 /**
32 * Implementation of hook_theme to tell Drupal what templates the engine
33 * and the current theme use. The $existing argument will contain hooks
34 * pre-defined by Drupal so that we can use that information if
35 * we need to.
36 */
37 function smarty_theme($existing, $type, $theme, $path) {
38 $templates = drupal_find_theme_functions($existing, array('smarty', $theme));
39 $extension = theme($extension);
40 $templates += drupal_find_theme_templates($existing, $extension, $path);
41 return $templates;
42 }
43
44 /**
45 * Render a smarty template.
46 *
47 * @param $file
48 * The filename of the template to render.
49 * @param $variables
50 * A keyed array of variables that will appear in the output.
51 *
52 * @return
53 * The output generated by the template.
54 */
55 function smarty_render_template($file, $variables) {
56
57 // if smarty is broken, this should make sure the admin can still see the
58 // menu to do something about it
59 if ( !_smarty_init() ) {
60 _smarty_engine_error('no_init');
61 return var_dump($variables);
62 }
63
64 // reuse the object to save time and allow 'global' smarty variables
65 $smarty = &_smarty_get_object();
66
67 // preserve previous smarty variables and add {$globals}
68 $old_tpl_vars = $smarty->_tpl_vars;
69 $smarty->_tpl_vars = $variables;
70 $smarty->_tpl_vars['globals'] = $old_tpl_vars['globals'];
71 $contents = $smarty->fetch($file);
72
73 // restore previous Smarty variables but preserve any changes to {$globals}
74 $old_tpl_vars['globals'] = $smarty->_tpl_vars['globals'];
75 $smarty->_tpl_vars = $old_tpl_vars;
76
77 return $contents;
78 }
79
80 /**
81 * @function _smarty_init
82 * Include the Smarty class and phptemplate.engine which are both required by
83 * smarty; also check writeablility of the templates_c directory. A static
84 * variable is used so we only do the checks once.
85 */
86 function _smarty_init() {
87 static $ok;
88
89 if ( isset($ok) ) {
90 return $ok;
91 }
92
93 $ok = TRUE;
94
95 // find the Smarty class
96 if (!class_exists('Smarty')) { // prevent redeclaration
97 $smarty_path = drupal_get_path('theme_engine', 'smarty') . '/libs/Smarty.class.php';
98 @include_once $smarty_path;
99 if ( !class_exists('Smarty') ) {
100 _smarty_engine_error('no_class');
101 $ok = FALSE;
102 }
103 }
104
105 // check the $compile_dir is writeable
106 $compile_dir = variable_get('smarty_compile_dir', drupal_get_path('theme_engine', 'smarty').'/templates_c');
107 if ( !is_writeable($compile_dir) ) {
108 _smarty_engine_error('not_writeable');
109 $ok = FALSE;
110 }
111
112 return $ok;
113 }
114
115 /**
116 * @function _smarty_engine_error
117 *
118 * Display a useful error message
119 *
120 * @param $error
121 * Shorthand for the error description
122 */
123 function _smarty_engine_error($error='default') {
124 $doclink = ' ' . t('Please refer to the documentation on the ').'<a href="http://drupal.org/project/smarty">' . t('Smarty project page') . '</a>.';
125 $errors = array(
126 'default' => t('There is an unknown problem with the smarty theme engine'),
127 'no_class' => t('The smarty theme engine could not find Smarty.class.php for inclusion.') . $doclink,
128 'not_writeable' => t('The smarty theme engine cannot write to the compiled template cache.') . $doclink,
129 'no_init' => t('The smarty theme engine cannot be loaded - change to another theme engine.') . $doclink,
130 );
131 if ( isset($errors[$error]) ) {
132 $msg = $errors[$error];
133 } else {
134 $msg = $errors['default'];
135 }
136 drupal_set_message($msg, 'error');
137 }
138
139
140 /**
141 * @function smarty_get_object
142 *
143 * Singleton generator. A singleton Smarty object is used to save resources
144 * re-registering plugins, and to enable 'global' variables to be passed
145 * between Smarty templates like {global var=myvar value=$localvar} which
146 * can then be accessed as {$global.myvar}
147 */
148 function &_smarty_get_object() {
149 static $smarty;
150
151 if ( !isset($smarty) ) {
152 $smarty = new Smarty;
153 $smarty->template_dir = realpath('./');
154 $smarty->compile_dir = variable_get('smarty_compile_dir', drupal_get_path('theme_engine', 'smarty').'/templates_c');
155 $smarty->compile_check = variable_get('smarty_compile_check', TRUE);
156 $smarty->config_dir = path_to_theme().'/configs';
157 // allow overloading of plugin file so look in the lowest level first
158 $smarty->plugins_dir = array(
159 path_to_theme().'/plugins',
160 drupal_get_path('theme_engine', 'smarty').'/plugins',
161 drupal_get_path('theme_engine', 'smarty') . '/libs/plugins',
162 );
163
164 // build list of standard plugins
165 $plugins = array(
166 'functions' => array(
167 'drupal' => '_smarty_plugin_function_drupal',
168 'global' => '_smarty_plugin_function_global',
169 ),
170 'modifiers' => array(
171 't' => 'smartyPlugin_modifier_t',
172 ),
173 );
174 // add theme plugins
175 $extra = theme('register_smarty_functions');
176 if ( is_array($extra) ) {
177 $plugins = array_merge($plugins, $extra);
178 }
179
180 // these are the types of plugin supported - other entries are ignored
181 $types = array(
182 'functions' =>'register_function',
183 'modifiers' =>'register_modifier',
184 );
185 foreach ( $types as $type=>$method ) {
186 foreach ( $plugins[$type] as $key=>$value ) {
187 if ( is_numeric($key) ) {
188 $key = $value; // handle single parameter
189 }
190 // register the plugin
191 $smarty->$method($key, $value);
192 }
193 }
194 }
195 return $smarty;
196 }
197
198 /**
199 * @function smartyPlugin_blah
200 *
201 * Smarty plugins to implement calls to the equivalent drupal functions within
202 * Smarty templates.
203 */
204 function smartyPlugin_modifier_t($params) {
205 return call_user_func_array('t', $params);
206 }
207
208 /**
209 * @function smartyPlugin_function_drupal
210 *
211 * Smarty plugin to implement a call to the Drupal theme() function within a
212 * Smarty template.
213 */
214 function _smarty_plugin_function_drupal($params, &$smarty) {
215 // TODO move the declarations into _smarty_init_plugins or whatever
216 $callback_functions = array(
217 'links' => '_smarty_drupal_callback_links',
218 );
219 $array_callback_functions = array(
220 'theme' => 1,
221 't' => 1,
222 'l' => 1,
223 'url' => 1,
224 'variable_get'=> 1,
225 'format_date' => 1,
226 );
227 $function = key($params);
228 if ( isset($callback_functions[$function]) ) {
229 if ( $callback_functions[$function]===1 ) {
230 $func = $function;
231 } else {
232 $func = $callback_functions[$function];
233 }
234 return call_user_func($func, $params);
235 }
236 if ( isset($array_callback_functions[$function]) ) {
237 if ( $array_callback_functions[$function]===1 ) {
238 $func = $function;
239 } else {
240 $func = $array_callback_functions[$function];
241 }
242 return call_user_func_array($func, $params);
243 }
244 }
245
246 /**
247 * @function smartyPlugin_function_theme_links
248 *
249 * Smarty plugin to return themed HTML for links.
250 */
251 function _smarty_drupal_callback_links($params) {
252 if ( isset($params['links']) ) {
253 if ( !isset($params['class']) ) {
254 $params['class'] = 'links';
255 }
256 if ( isset($params['id']) ) {
257 $attribs = array('class' =>$params['class'], 'id' => $params['id']);
258 } else {
259 $attribs = array('class' =>$params['class']);
260 }
261 return theme('links', $params['links'], $attribs);
262 } else {
263 return '';
264 }
265 }
266
267 /**
268 * @function smartyPlugin_function_global
269 *
270 * Smarty plugin to implement global template variables.
271 */
272 function _smarty_plugin_function_global($params, &$smarty) {
273 if ( isset($params['var']) ) {
274 if ( isset($params['value']) ) {
275 $smarty->_tpl_vars['globals'][$params['var']] = $params['value'];
276 } elseif ( isset($smarty->_tpl_vars['globals'][$params['var']]) ) {
277 unset($smarty->_tpl_vars['globals'][$params['var']]);
278 }
279 }
280 }

  ViewVC Help
Powered by ViewVC 1.1.2