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

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

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

revision 1.14, Sat Apr 14 02:40:09 2007 UTC revision 1.15, Fri Oct 19 17:44:34 2007 UTC
# Line 1  Line 1 
1  <?php // $Id: smarty.engine,v 1.13 2007/04/14 02:21:28 djnz Exp $  <?php
2    // $Id: phptemplate.engine,v 1.69 2007/10/02 16:19:23 dries Exp $
3    
4  /**  /**
5   * @file   * @file
6   * Handles integration of Smarty templates with the Drupal theme system.   * 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   * @function _smarty_init
82   * Include the Smarty class and phptemplate.engine which are both required by   * Include the Smarty class and phptemplate.engine which are both required by
# Line 18  function _smarty_init() { Line 90  function _smarty_init() {
90      return $ok;      return $ok;
91    }    }
92    
93    $doclink = t(' Please refer to the documentation on the <a href="http://drupal.org/project/smarty">Smarty project page</a>.');    $ok = TRUE;
   
   // phptemplate functions are inherited wherever possible  
   $phptemplate_path = drupal_get_path('theme_engine', 'phptemplate') . '/phptemplate.engine';  
   if ( file_exists($phptemplate_path) ) {  
     include_once $phptemplate_path;  
   } else {  
     drupal_set_message(t('The smarty theme engine could not find phptemplate.engine for inclusion.').$doclink, 'error');  
     $ok = FALSE;  
   }  
94    
95    // find the Smarty class    // find the Smarty class
96    if (!class_exists('Smarty')) {  // prevent redeclaration    if (!class_exists('Smarty')) {  // prevent redeclaration
97      $smarty_path = drupal_get_path('theme_engine', 'smarty') . '/libs/Smarty.class.php';      $smarty_path = drupal_get_path('theme_engine', 'smarty') . '/libs/Smarty.class.php';
98      @include_once $smarty_path;      @include_once $smarty_path;
99      if ( !class_exists('Smarty') ) {      if ( !class_exists('Smarty') ) {
100        drupal_set_message(t('The smarty theme engine could not find Smarty.class.php for inclusion.').$doclink, 'error');        _smarty_engine_error('no_class');
101        $ok = FALSE;        $ok = FALSE;
102      }      }
103    }    }
104    
105    $compile_dir = dirname(__FILE__).'/templates_c';    // 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) ) {    if ( !is_writeable($compile_dir) ) {
108      drupal_set_message(t('The smarty theme engine cannot write to the compiled template cache.').$doclink, 'error');      _smarty_engine_error('not_writeable');
109      $ok = FALSE;      $ok = FALSE;
110    }    }
   if ( $ok!==FALSE ) {  
     $ok = TRUE;  
   }  
   return $ok;  
 }  
   
  function smarty_init($template) {  
   _smarty_init();  
   return phptemplate_init($template);  
 }  
111    
112  function smarty_templates($directory = 'themes') {    return $ok;
   // only return smarty themes if there is no problem with initialisation  
   if ( _smarty_init() ) {  
     return drupal_system_listing('^page\.tpl$', $directory, 'filename');  
   } else {  
     return array();  
   }  
113  }  }
114    
115  /**  /**
116   * @function _smarty_   * @function _smarty_engine_error
117   *   *
118   */   * Display a useful error message
 function smarty_regions() {  
   return phptemplate_regions();  
 }  
   
 function smarty_features() {  
   return phptemplate_features();  
 }  
   
 function smarty_page($content, $show_blocks = TRUE) {  
   return phptemplate_page($content, $show_blocks);  
 }  
   
 function smarty_node($node, $teaser = 0, $page = 0) {  
   return phptemplate_node($node, $teaser, $page);  
 }  
   
 function smarty_comment($comment, $links = 0) {  
   return phptemplate_comment($comment, $links);  
 }  
   
 function smarty_block($block) {  
   return phptemplate_block($block);  
 }  
   
 function smarty_box($title, $content, $region = 'main') {  
   return phptemplate_box($title, $content, $region);  
 }  
   
 /**  
  * @function _smarty_default  
119   *   *
120   * Render a template file, setting the supplied variables   * @param $error
121     *   Shorthand for the error description
122   */   */
123  // This is the only chance to add in some global theme variables (should be implemented in PHPtemplate)  function _smarty_engine_error($error='default') {
124  // Also use the default of .tpl instead of .tpl.php    $doclink = ' ' . t('Please refer to the documentation on the ').'<a href="http://drupal.org/project/smarty">' . t('Smarty project page') . '</a>.';
125  function _smarty_default($hook, $variables, $suggestions = array(), $extension = '.tpl') {    $errors = array(
126    $variables['user'] = $GLOBALS['user'];        'default'       => t('There is an unknown problem with the smarty theme engine'),
127    $variables['base_path'] = base_path();        'no_class'      => t('The smarty theme engine could not find Smarty.class.php for inclusion.') . $doclink,
128    $variables['path_to_theme'] = path_to_theme();        'not_writeable' => t('The smarty theme engine cannot write to the compiled template cache.') . $doclink,
129    return _phptemplate_default($hook, $variables, $suggestions, $extension);        '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   * @function smarty_get_object
142   *   *
143   * Singleton generator. A singleton Smarty object is used to save resources   * Singleton generator. A singleton Smarty object is used to save resources
144   * re-registering plugins, and to enable 'global' variables to be passed   * re-registering plugins, and to enable 'global' variables to be passed
145   * between Smarty templates like {global var=myvar value=$localvar}   * between Smarty templates like {global var=myvar value=$localvar} which
146   *   * can then be accessed as {$global.myvar}
147   */   */
148  function &smarty_get_object() {  function &_smarty_get_object() {
149    static $smarty;    static $smarty;
150    
151    if ( !isset($smarty) ) {    if ( !isset($smarty) ) {
152      $smarty = new Smarty;      $smarty = new Smarty;
153      $smarty->template_dir = realpath('./');      $smarty->template_dir = realpath('./');
154      $smarty->compile_dir  = variable_get('smarty_compile_dir', path_to_engine().'/templates_c');      $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);      $smarty->compile_check = variable_get('smarty_compile_check', TRUE);
156      $smarty->config_dir   = path_to_theme().'/configs';      $smarty->config_dir   = path_to_theme().'/configs';
157      $smarty->plugins_dir  = array(path_to_engine() . '/libs/plugins', path_to_engine().'/plugins', path_to_theme().'/plugins');      $smarty->plugins_dir  = array(drupal_get_path('theme_engine', 'smarty') . '/libs/plugins', drupal_get_path('theme_engine', 'smarty').'/plugins', path_to_theme().'/plugins'); // TODO add theme directory
158    
159      // register plugins, including theme-specific ones      // build list of standard plugins
160      $plugins = array(      $plugins = array(
161        'functions' => array(        'functions' => array(
162          'theme'       => 'smarty_function_theme',          'theme'       => 'smartyPlugin_function_theme',
163          'theme_links' => 'smarty_function_theme_links',          'theme_links' => 'smartyPlugin_function_theme_links',
164            'global'      => 'smartyPlugin_function_global',
165            'l'           => 'smartyPlugin_function_l',
166            't'           => 'smartyPlugin_function_t',
167            'url'         => 'smartyPlugin_function_url',
168            'variable_get'=> 'smartyPlugin_function_get',
169        ),        ),
170        'modifiers' => array(        'modifiers' => array(
171            't'           => 'smartyPlugin_modifier_t',
172        ),        ),
173      );      );
174        // add theme plugins
175      $extra = theme('register_smarty_functions');      $extra = theme('register_smarty_functions');
176      if ( is_array($extra) ) {      if ( is_array($extra) ) {
177        $plugins = array_merge($plugins, $extra);        $plugins = array_merge($plugins, $extra);
178      }      }
179    
180        // these are the types of plugin supported - other entries are ignored
181      $types = array(      $types = array(
182        'functions' =>'register_function',        'functions' =>'register_function',
183        'modifiers' =>'register_modifier',        'modifiers' =>'register_modifier',
# Line 152  function &smarty_get_object() { Line 187  function &smarty_get_object() {
187          if ( is_numeric($key) ) {          if ( is_numeric($key) ) {
188            $key = $value; // handle single parameter            $key = $value; // handle single parameter
189          }          }
190            // register the plugin
191          $smarty->$method($key, $value);          $smarty->$method($key, $value);
192        }        }
193      }      }
194    }    }
195    return $smarty;    return $smarty;
   
196  }  }
197    
198  /**  /**
199   * @function _smarty_render   * @function smartyPlugin_blah
200   *   *
201   * Render a template file, setting the supplied variables   * Smarty plugins to implement calls to the equivalent drupal functions within
202     * Smarty templates.
203   */   */
204  function _smarty_render($file, $variables) {  function smartyPlugin_function_l($params) {
205      return call_user_func_array('l', $params);
206    // reuse the node to save time and allow 'global' smarty variables  }
207    $smarty = &smarty_get_object();  function smartyPlugin_function_t($params) {
208      return call_user_func_array('t', $params);
209    // preserve previous smarty variables and add {$globals}  }
210    $old_tpl_vars = $smarty->_tpl_vars;  function smartyPlugin_function_url($params) {
211    $smarty->_tpl_vars = $variables;    return call_user_func_array('url', $params);
212    $smarty->_tpl_vars['globals'] = $old_tpl_vars['globals'];  }
213    function smartyPlugin_function_variable_get($params) {
214    $contents = $smarty->fetch($file);    return call_user_func_array('variable_get', $params);
215    }
216    // restore previous Smarty variables but preserve any changes to {$globals}  function smartyPlugin_modifier_t($params) {
217    $old_tpl_vars['globals'] = $smarty->_tpl_vars['globals'];    return call_user_func_array('t', $params);
   $smarty->_tpl_vars = $old_tpl_vars;  
   
   return $contents;  
218  }  }
219    
220  /**  /**
221   * @function smarty_function_theme   * @function smartyPlugin_function_theme
222   *   *
223   * Smarty plugin to implement a call to the Drupal theme() function within a   * Smarty plugin to implement a call to the Drupal theme() function within a
224   * Smarty template.   * Smarty template.
225   */   */
226  function smarty_function_theme($params, &$smarty) {  function smartyPlugin_function_theme($params, &$smarty) {
227    $function = array_shift($params);    $function = array_shift($params);
228    if ($func = theme_get_function($function)) {    if ($func = theme_get_function($function)) {
229      return call_user_func_array($func, $params);      return call_user_func_array($func, $params);
# Line 198  function smarty_function_theme($params, Line 231  function smarty_function_theme($params,
231  }  }
232    
233  /**  /**
234   * @function smarty_function_theme_links   * @function smartyPlugin_function_theme_links
235   *   *
236   * Smarty plugin to return themed HTML for links   * Smarty plugin to return themed HTML for links.
237   */   */
238  function smarty_function_theme_links($params, &$smarty) {  function smartyPlugin_function_theme_links($params, &$smarty) {
239    if ( isset($params['links']) ) {    if ( isset($params['links']) ) {
240      if ( !isset($params['class']) ) {      if ( !isset($params['class']) ) {
241        $params['class'] = 'links';        $params['class'] = 'links';
# Line 212  function smarty_function_theme_links($pa Line 245  function smarty_function_theme_links($pa
245      } else {      } else {
246        $attribs = array('class' =>$params['class']);        $attribs = array('class' =>$params['class']);
247      }      }
248        // $params['links'] is now a pointer to the variable containing the links
249      return theme('links', $params['links'], $attribs);      return theme('links', $params['links'], $attribs);
250    } else {    } else {
251      return '';      return '';
# Line 219  function smarty_function_theme_links($pa Line 253  function smarty_function_theme_links($pa
253  }  }
254    
255  /**  /**
256   * @function smarty_function_global   * @function smartyPlugin_function_global
257   *   *
258   * Smarty plugin to implement global template variables   * Smarty plugin to implement global template variables.
259   */   */
260  function smarty_function_global($params, &$smarty) {  function smartyPlugin_function_global($params, &$smarty) {
261    if ( isset($params['var']) ) {    if ( isset($params['var']) ) {
262      if ( isset($params['value']) ) {      if ( isset($params['value']) ) {
263        $smarty->_tpl_vars['globals'][$params['var']] = $params['value'];        $smarty->_tpl_vars['globals'][$params['var']] = $params['value'];
264      } else {      } elseif ( isset($smarty->_tpl_vars['globals'][$params['var']]) ) {
265        unset($smarty->_tpl_vars['globals'][$params['var']]);        unset($smarty->_tpl_vars['globals'][$params['var']]);
266      }      }
267    }    }

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.15

  ViewVC Help
Powered by ViewVC 1.1.2