| 1 |
<?php |
<?php // $Id: smarty.engine,v 1.1 2007/04/13 23:09:41 djnz Exp $ |
|
// $Id: smarty.engine,v 1.11 2005/12/31 09:38:51 tclineks Exp $ |
|
| 2 |
|
|
| 3 |
/** |
/** |
| 4 |
* @file |
* @file |
| 5 |
* Handles integration of templates written in Smarty ( @link http://www.smarty.net ) |
* Handles integration of Smarty templates with the Drupal theme system. |
|
* with the Drupal ( @link http://www.drupal.org ) theme system. |
|
| 6 |
*/ |
*/ |
| 7 |
|
|
|
require_once 'inc/common.php'; |
|
|
|
|
|
function smarty_init($template) { |
|
|
require_once realpath('themes/engines/smarty/smartytemplate.php'); |
|
|
$file = dirname($template->filename) . '/smartytemplate.php'; |
|
|
if (file_exists($file)) { |
|
|
include_once "./$file"; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Returns an array of templates that should be parsed with this (Smarty) engine. |
|
|
* called from system.module:system_theme_data() |
|
|
*/ |
|
|
function smarty_templates($directory = 'themes') { |
|
|
_test_permissions(); |
|
|
return system_listing('^page\.tpl$', $directory, 'filename'); |
|
|
} |
|
|
|
|
| 8 |
/** |
/** |
| 9 |
* Declare the available regions implemented by this engine. |
* @function _smarty_init |
| 10 |
* |
* Include the Smarty class and phptemplate.engine which are both required by |
| 11 |
* @return |
* smarty; also check writeablility of the templates_c directory. A static |
| 12 |
* An array of regions. The first array element will be used as the default region for themes. |
* variable is used so we only do the checks once. |
| 13 |
*/ |
*/ |
| 14 |
function smarty_regions() { |
function _smarty_init() { |
| 15 |
return array( |
static $ok; |
|
'left' => t('left sidebar'), |
|
|
'right' => t('right sidebar'), |
|
|
'content' => t('content'), |
|
|
'header' => t('header'), |
|
|
'footer' => t('footer') |
|
|
); |
|
|
} |
|
| 16 |
|
|
| 17 |
/** |
if ( isset($ok) ) { |
| 18 |
* Execute a template engine call. |
return $ok; |
| 19 |
* |
} |
|
* Each call to the template engine has two parts. Namely preparing |
|
|
* the variables, and then doing something with them. |
|
|
* |
|
|
* The first step is done by all template engines / themes, the second |
|
|
* step is dependent on the engine used. |
|
|
* |
|
|
* @param $hook |
|
|
* The name of the theme function being executed |
|
|
* @param $variables |
|
|
* A sequential array of variables passed to the theme function. |
|
|
* @param $file |
|
|
* A suggested template file to use. If the file is not found, the default $hook.tpl will be used. |
|
|
* @return |
|
|
* The HTML generated by the template system. |
|
|
*/ |
|
|
function _smarty_callback($hook, $variables = array(), $file = NULL) { |
|
| 20 |
|
|
| 21 |
$variables = array_merge($variables, _smarty_default_variables($hook, $variables)); |
$doclink = t(' Please refer to the documentation on the <a href="http://drupal.org/project/smarty">Smarty project page</a>.'); |
| 22 |
|
|
| 23 |
// Allow specified variables to be overridden |
// phptemplate functions are inherited wherever possible |
| 24 |
if (function_exists('_smarty_variables')) { |
$phptemplate_path = drupal_get_path('theme_engine', 'phptemplate') . '/phptemplate.engine'; |
| 25 |
$variables = array_merge($variables, _smarty_variables($hook, $variables)); |
if ( file_exists($phptemplate_path) ) { |
| 26 |
|
include_once $phptemplate_path; |
| 27 |
|
} else { |
| 28 |
|
drupal_set_message(t('The smarty theme engine could not find phptemplate.engine for inclusion.').$doclink, 'error'); |
| 29 |
|
$ok = FALSE; |
| 30 |
} |
} |
| 31 |
|
|
| 32 |
if (isset($variables['template_file'])) { |
// find the Smarty class |
| 33 |
$file = $variables['template_file']; |
if (!class_exists('Smarty')) { // prevent redeclaration |
| 34 |
|
$smarty_path = drupal_get_path('theme_engine', 'smarty') . '/libs/Smarty.class.php'; |
| 35 |
|
@include_once $smarty_path; |
| 36 |
|
if ( !class_exists('Smarty') ) { |
| 37 |
|
drupal_set_message(t('The smarty theme engine could not find Smarty.class.php for inclusion.').$doclink, 'error'); |
| 38 |
|
$ok = FALSE; |
| 39 |
|
} |
| 40 |
} |
} |
| 41 |
|
|
| 42 |
// Call associated theme function or pass to _smarty_default |
$compile_dir = dirname(__FILE__).'/templates_c'; |
| 43 |
if (function_exists('_smarty_' . $hook)) { |
|
| 44 |
return call_user_func('_smarty_' . $hook, $variables, $file); |
if ( !is_writeable($compile_dir) ) { |
| 45 |
|
drupal_set_message(t('The smarty theme engine cannot write to the compiled template cache.').$doclink, 'error'); |
| 46 |
|
$ok = FALSE; |
| 47 |
} |
} |
| 48 |
elseif (function_exists('_smarty_default')) { |
if ( $ok!==FALSE ) { |
| 49 |
return call_user_func('_smarty_default', $hook, $variables, $file); |
$ok = TRUE; |
| 50 |
} |
} |
| 51 |
|
return $ok; |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
function smarty_init($template) { |
| 55 |
|
_smarty_init(); |
| 56 |
|
return phptemplate_init($template); |
| 57 |
|
} |
| 58 |
|
|
| 59 |
|
function smarty_templates($directory = 'themes') { |
| 60 |
|
// only return smarty themes if there is no problem with initialisation |
| 61 |
|
if ( _smarty_init() ) { |
| 62 |
|
return drupal_system_listing('^page\.tpl$', $directory, 'filename'); |
| 63 |
|
} else { |
| 64 |
|
return array(); |
| 65 |
|
} |
| 66 |
} |
} |
| 67 |
|
|
| 68 |
/** |
/** |
| 69 |
* Adds additional helper variables to all templates. |
* @function _smarty_ |
|
* |
|
|
* Counts how many times certain hooks have been called. Sidebar left / right are specials cases. |
|
| 70 |
* |
* |
|
* @param $hook |
|
|
* The name of the theme function being executed. |
|
|
* @param $variables |
|
|
* A sequential array of variables passed to the theme function. |
|
| 71 |
*/ |
*/ |
| 72 |
function _smarty_default_variables($hook, $variables) { |
function smarty_regions() { |
| 73 |
global $theme; |
return phptemplate_regions(); |
| 74 |
static $count = array(); |
} |
|
$count[$hook] = isset($count[$hook]) && is_int($count[$hook]) ? $count[$hook] : 1; |
|
|
$variables['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even'; |
|
|
$variables['id'] = $count[$hook]++; |
|
|
|
|
|
global $sidebar_indicator; |
|
|
if ($hook == 'block') { |
|
|
$count['block_counter'][$sidebar_indicator] = isset($count['block_counter'][$sidebar_indicator]) && is_int($count['block_counter'][$sidebar_indicator]) ? $count['block_counter'][$sidebar_indicator] : 1; |
|
|
$variables['block_zebra'] = ($count['block_counter'][$sidebar_indicator] % 2) ? 'odd' : 'even'; |
|
|
$variables['block_id'] = $count['block_counter'][$sidebar_indicator]++; |
|
|
} |
|
|
elseif ($hook == 'page') { |
|
|
$regions = system_region_list($theme); |
|
|
// Load all region content assigned via blocks. |
|
|
foreach (array_keys($regions) as $region) { |
|
|
// Skip blocks in this region that have already been loaded. |
|
|
// This pre-loading is necessary because phptemplate uses variable names different from |
|
|
// the region names, e.g., 'sidebar_left' instead of 'left'. |
|
|
if (!in_array($region, array('left', 'right', 'footer'))) { |
|
|
isset($variables[$region]) ? $variables[$region] .= theme('blocks', $region) : $variables[$region] = theme('blocks', $region); |
|
|
} |
|
|
} |
|
|
} |
|
|
// Tell all templates where they are located. |
|
|
$variables['directory'] = path_to_theme(); |
|
| 75 |
|
|
| 76 |
if (drupal_get_path_alias($_GET['q']) == variable_get('site_frontpage', 'node')) { |
function smarty_features() { |
| 77 |
$variables['is_front'] = true; |
return phptemplate_features(); |
| 78 |
} |
} |
| 79 |
|
|
| 80 |
|
function smarty_page($content, $show_blocks = TRUE) { |
| 81 |
|
return phptemplate_page($content, $show_blocks); |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
function smarty_node($node, $teaser = 0, $page = 0) { |
| 85 |
|
return phptemplate_node($node, $teaser, $page); |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
function smarty_comment($comment, $links = 0) { |
| 89 |
|
return phptemplate_comment($comment, $links); |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
function smarty_block($block) { |
| 93 |
|
return phptemplate_block($block); |
| 94 |
|
} |
| 95 |
|
|
| 96 |
return $variables; |
function smarty_box($title, $content, $region = 'main') { |
| 97 |
|
return phptemplate_box($title, $content, $region); |
| 98 |
} |
} |
| 99 |
|
|
| 100 |
/** |
/** |
| 101 |
* @return |
* @function _smarty_default |
| 102 |
* Array of template features |
* |
| 103 |
|
* Render a template file, setting the supplied variables |
| 104 |
*/ |
*/ |
| 105 |
function smarty_features() { |
// This is the only chance to add in some global theme variables (should be implemented in PHPtemplate) |
| 106 |
return array( |
// Also use the default of .tpl instead of .tpl.php |
| 107 |
'logo', |
function _smarty_default($hook, $variables, $suggestions = array(), $extension = '.tpl') { |
| 108 |
'toggle_comment_user_picture', |
$variables['user'] = $GLOBALS['user']; |
| 109 |
'toggle_favicon', |
$variables['base_path'] = base_path(); |
| 110 |
'toggle_mission', |
$variables['path_to_theme'] = path_to_theme(); |
| 111 |
'toggle_name', |
return _phptemplate_default($hook, $variables, $suggestions, $extension); |
|
'toggle_node_user_picture', |
|
|
'toggle_search', |
|
|
'toggle_slogan' |
|
|
); |
|
| 112 |
} |
} |
| 113 |
|
|
| 114 |
/** |
/** |
| 115 |
* Prepare the values passed to the theme_page function to be passed |
* @function smarty_get_object |
| 116 |
* into a pluggable template engine. |
* |
| 117 |
|
* Singleton generator. A singleton Smarty object is used to save resources |
| 118 |
|
* re-registering plugins, and to enable 'global' variables to be passed |
| 119 |
|
* between Smarty templates like {global var=myvar value=$localvar} |
| 120 |
|
* |
| 121 |
*/ |
*/ |
| 122 |
function smarty_page($content) { |
function &smarty_get_object() { |
| 123 |
/* Set title and breadcrumb to declared values */ |
static $smarty; |
|
|
|
|
if ($_GET['q'] == variable_get('site_frontpage', 'node')) { |
|
|
$mission = theme_get_setting('mission'); |
|
|
$frontpage = true; |
|
|
} |
|
|
|
|
|
/* Add favicon */ |
|
|
if (theme_get_setting('toggle_favicon')) { |
|
|
drupal_set_html_head('<link rel="shortcut icon" href="'. check_url(theme_get_setting('favicon')) .'" type="image/x-icon" />'); |
|
|
} |
|
| 124 |
|
|
| 125 |
/** |
if ( !isset($smarty) ) { |
| 126 |
* Populate sidebars. |
$smarty = new Smarty; |
| 127 |
*/ |
$smarty->template_dir = realpath('./'); |
| 128 |
$layout = "none"; |
$smarty->compile_dir = variable_get('smarty_compile_dir', path_to_engine().'/templates_c'); |
| 129 |
global $sidebar_indicator; |
$smarty->compile_check = variable_get('smarty_compile_check', TRUE); |
| 130 |
/** |
$smarty->config_dir = path_to_theme().'/configs'; |
| 131 |
* Sidebar_indicator tells the block counting code to count sidebars seperately. |
$smarty->plugins_dir = array(path_to_engine() . '/libs/plugins', path_to_engine().'/plugins', path_to_theme().'/plugins'); |
|
*/ |
|
|
$sidebar_indicator = 'left'; |
|
|
$sidebar_left = theme('blocks', 'left'); |
|
|
if ($sidebar_left != '') { |
|
|
$layout = 'left'; |
|
|
} |
|
|
|
|
|
$sidebar_indicator = 'right'; |
|
|
$sidebar_right = theme('blocks', 'right'); |
|
|
if ($sidebar_right != '') { |
|
|
$layout = ($layout == 'left') ? 'both' : 'right'; |
|
|
} |
|
|
$sidebar_indicator = NULL; |
|
| 132 |
|
|
| 133 |
// Construct page title |
// register plugins, including theme-specific ones |
| 134 |
if (drupal_get_title()) { |
$plugins = array( |
| 135 |
$head_title = array(strip_tags(drupal_get_title()), variable_get('site_name', 'drupal')); |
'functions' => array( |
| 136 |
} |
'theme' => 'smarty_function_theme', |
| 137 |
else { |
'theme_links' => 'smarty_function_theme_links', |
| 138 |
$head_title = array(variable_get('site_name', 'drupal')); |
), |
| 139 |
if (variable_get('site_slogan', '')) { |
'modifiers' => array( |
| 140 |
$head_title[] = variable_get('site_slogan', ''); |
), |
| 141 |
|
); |
| 142 |
|
$extra = theme('register_smarty_functions'); |
| 143 |
|
if ( is_array($extra) ) { |
| 144 |
|
$plugins = array_merge($plugins, $extra); |
| 145 |
|
} |
| 146 |
|
$types = array( |
| 147 |
|
'functions' =>'register_function', |
| 148 |
|
'modifiers' =>'register_modifier', |
| 149 |
|
); |
| 150 |
|
foreach ( $types as $type=>$method ) { |
| 151 |
|
foreach ( $plugins[$type] as $key=>$value ) { |
| 152 |
|
if ( is_numeric($key) ) { |
| 153 |
|
$key = $value; // handle single parameter |
| 154 |
|
} |
| 155 |
|
$smarty->$method($key, $value); |
| 156 |
|
} |
| 157 |
} |
} |
| 158 |
} |
} |
| 159 |
|
return $smarty; |
| 160 |
|
|
|
$variables = array( |
|
|
'breadcrumb' => theme('breadcrumb', drupal_get_breadcrumb()), |
|
|
'closure' => theme('closure'), |
|
|
'content' => '<!-- begin content -->' . $content . '<!-- end content -->', |
|
|
'footer_message' => variable_get('site_footer', FALSE) . "\n" . theme('blocks', 'footer'), |
|
|
'head' => drupal_get_html_head(), |
|
|
'head_title' => implode(' | ', $head_title), |
|
|
'help' => theme('help'), |
|
|
'language' => $GLOBALS['locale'], |
|
|
'layout' => $layout, |
|
|
'logo' => theme_get_setting('logo'), |
|
|
'messages' => theme('status_messages'), |
|
|
'mission' => isset($mission) ? $mission : '', |
|
|
'onload_attributes' => theme('onload_attribute'), |
|
|
'primary_links' => menu_primary_links(), |
|
|
'search_box' => (theme_get_setting('toggle_search') ? search_box() : ''), |
|
|
'secondary_links' => menu_secondary_links(), |
|
|
'sidebar_left' => $sidebar_left, |
|
|
'sidebar_right' => $sidebar_right, |
|
|
'site_name' => (theme_get_setting('toggle_name') ? variable_get('site_name', 'Drupal') : ''), |
|
|
'site_slogan' => (theme_get_setting('toggle_slogan') ? variable_get('site_slogan', '') : ''), |
|
|
'styles' => theme_get_styles(), |
|
|
'tabs' => theme('menu_local_tasks'), |
|
|
'title' => drupal_get_title() |
|
|
); |
|
|
|
|
|
if ((arg(0) == 'node') && is_numeric(arg(1))) { |
|
|
$variables['node'] = node_load(arg(1)); |
|
|
} |
|
|
|
|
|
return _smarty_callback('page', $variables); |
|
| 161 |
} |
} |
| 162 |
|
|
| 163 |
/** |
/** |
| 164 |
* Prepare the values passed to the theme_node function to be passed |
* @function _smarty_render |
| 165 |
* into a pluggable template engine. |
* |
| 166 |
|
* Render a template file, setting the supplied variables |
| 167 |
*/ |
*/ |
| 168 |
function smarty_node($node, $teaser = 0, $page = 0) { |
function _smarty_render($file, $variables) { |
|
if (module_exist('taxonomy')) { |
|
|
$taxonomy = taxonomy_link('taxonomy terms', $node); |
|
|
} |
|
|
else { |
|
|
$taxonomy = array(); |
|
|
} |
|
| 169 |
|
|
| 170 |
$variables = array( |
// reuse the node to save time and allow 'global' smarty variables |
| 171 |
'content' => ($teaser && $node->teaser) ? $node->teaser : $node->body, |
$smarty = &smarty_get_object(); |
|
'date' => format_date($node->created), |
|
|
'links' => $node->links ? theme('links', $node->links) : '', |
|
|
'name' => theme('username', $node), |
|
|
'node' => $node, // we pass the actual node to allow more customization |
|
|
'node_url' => url('node/'. $node->nid), |
|
|
'page' => $page, |
|
|
'taxonomy' => $taxonomy, |
|
|
'teaser' => $teaser, |
|
|
'terms' => theme('links', $taxonomy), |
|
|
'title' => check_plain($node->title) |
|
|
); |
|
|
|
|
|
// Flatten the node object's member fields. |
|
|
$variables = array_merge((array) $node, $variables); |
|
|
|
|
|
// Display info only on certain node types. |
|
|
if (theme_get_setting('toggle_node_info_' . $node->type)) { |
|
|
$variables['submitted'] = t('Submitted by %a on %b.', array('%a' => theme('username', $node), '%b' => format_date($node->created))); |
|
|
$variables['picture'] = theme_get_setting('toggle_node_user_picture') ? theme('user_picture', $node) : ''; |
|
|
} |
|
| 172 |
|
|
| 173 |
return _smarty_callback('node', $variables, 'node-' . $node->type); |
// preserve previous smarty variables and add {$globals} |
| 174 |
} |
$old_tpl_vars = $smarty->_tpl_vars; |
| 175 |
|
$smarty->_tpl_vars = $variables; |
| 176 |
|
$smarty->_tpl_vars['globals'] = $old_tpl_vars['globals']; |
| 177 |
|
|
| 178 |
/** |
$contents = $smarty->fetch($file); |
|
* Prepare the values passed to the theme_comment function to be passed |
|
|
* into a pluggable template engine. |
|
|
*/ |
|
|
function smarty_comment($comment, $links = 0) { |
|
|
return _smarty_callback('comment', array( |
|
|
'author' => theme('username', $comment), |
|
|
'comment' => $comment, |
|
|
'content' => $comment->comment, |
|
|
'date' => format_date($comment->timestamp), |
|
|
'links' => isset($links) ? theme('links', $links) : '', |
|
|
'new' => $comment->new ? t('new') : '', |
|
|
'picture' => theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', $comment) : '', |
|
|
'submitted' => t('Submitted by %a on %b.', |
|
|
array('%a' => theme('username', $comment), |
|
|
'%b' => format_date($comment->timestamp))), |
|
|
'title' => l($comment->subject, $_GET['q'], NULL, NULL, "comment-$comment->cid") |
|
|
)); |
|
|
} |
|
| 179 |
|
|
| 180 |
/** |
// restore previous Smarty variables but preserve any changes to {$globals} |
| 181 |
* Prepare the values passed to the theme_block function to be passed |
$old_tpl_vars['globals'] = $smarty->_tpl_vars['globals']; |
| 182 |
* into a pluggable template engine. |
$smarty->_tpl_vars = $old_tpl_vars; |
| 183 |
*/ |
|
| 184 |
function smarty_block($block) { |
return $contents; |
|
return _smarty_callback('block', array('block' => $block)); |
|
| 185 |
} |
} |
| 186 |
|
|
| 187 |
/** |
/** |
| 188 |
* Prepare the values passed to the theme_box function to be passed |
* @function smarty_function_theme |
| 189 |
* into a pluggable template engine. |
* |
| 190 |
|
* Smarty plugin to implement a call to the Drupal theme() function within a |
| 191 |
|
* Smarty template. |
| 192 |
*/ |
*/ |
| 193 |
function smarty_box($title, $content, $region = 'main') { |
function smarty_function_theme($params, &$smarty) { |
| 194 |
return _smarty_callback('box', array( |
$function = array_shift($params); |
| 195 |
'content' => $content, |
if ($func = theme_get_function($function)) { |
| 196 |
'region' => $region, |
return call_user_func_array($func, $params); |
| 197 |
'title' => $title |
} |
|
)); |
|
| 198 |
} |
} |
| 199 |
|
|
| 200 |
/** |
/** |
| 201 |
* Default callback for Smarty Theme Engine. |
* @function smarty_function_theme_links |
| 202 |
* |
* |
| 203 |
* Load a template file, and pass the variable array to it. |
* Smarty plugin to return themed HTML for links |
|
* If the suggested file is not found, Smarty will attempt to use |
|
|
* a $hook.tpl file in the template directory, and failing that a |
|
|
* $hook.tpl in the Smarty directory. |
|
|
* |
|
|
* @param $hook |
|
|
* The name of the theme function being executed. |
|
|
* @param $variables |
|
|
* A sequential array of variables passed to the theme function. |
|
|
* @param $file |
|
|
* A suggested template file to use. |
|
| 204 |
*/ |
*/ |
| 205 |
function _smarty_default($hook, $variables, $file = NULL) { |
function smarty_function_theme_links($params, &$smarty) { |
| 206 |
global $smarty; |
if ( isset($params['links']) ) { |
| 207 |
|
if ( !isset($params['class']) ) { |
| 208 |
if (!empty($file) && file_exists(path_to_theme() . "/$file.tpl")) { |
$params['class'] = 'links'; |
|
$file = path_to_theme() . "/$file.tpl"; |
|
|
} |
|
|
else { |
|
|
if (file_exists(path_to_theme() . "/$hook.tpl")) { |
|
|
$file = path_to_theme() . "/$hook.tpl"; |
|
| 209 |
} |
} |
| 210 |
else { |
if ( isset($params['id']) ) { |
| 211 |
if (in_array($hook, array('node', 'block', 'box', 'comment'))) { |
$attribs = array('class' =>$params['class'], 'id' => $params['id']); |
| 212 |
$file = "themes/engines/smarty/$hook.tpl"; |
} else { |
| 213 |
} |
$attribs = array('class' =>$params['class']); |
|
else { |
|
|
$variables['hook'] = $hook; |
|
|
watchdog('error', t('Smarty Theme Engine was instructed to override the %name theme function, but no valid template file was found.', array('%name' => theme('placeholder', $hook)))); |
|
|
$file = 'themes/engines/smarty/default.tpl'; |
|
|
} |
|
| 214 |
} |
} |
| 215 |
|
return theme('links', $params['links'], $attribs); |
| 216 |
|
} else { |
| 217 |
|
return ''; |
| 218 |
} |
} |
| 219 |
|
} |
| 220 |
|
|
| 221 |
if (isset($file)) { |
/** |
| 222 |
$smarty[$hook] = new SmartyTemplate(dirname($file)); |
* @function smarty_function_global |
| 223 |
$smarty[$hook]->set_vars($variables); |
* |
| 224 |
return $smarty[$hook]->fetch(basename($file)); |
* Smarty plugin to implement global template variables |
| 225 |
|
*/ |
| 226 |
|
function smarty_function_global($params, &$smarty) { |
| 227 |
|
if ( isset($params['var']) ) { |
| 228 |
|
if ( isset($params['value']) ) { |
| 229 |
|
$smarty->_tpl_vars['globals'][$params['var'] = $params['value']; |
| 230 |
|
} else { |
| 231 |
|
unset($smarty->_tpl_vars['globals'][$params['var']); |
| 232 |
|
} |
| 233 |
} |
} |
|
|
|
| 234 |
} |
} |
|
|
|
|
?> |
|