| 1 |
<?php
|
| 2 |
// $Id: Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Handles integration of Smarty templates with the Drupal theme system.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* @function _smarty_init
|
| 11 |
* Include the Smarty class and phptemplate.engine which are both required by
|
| 12 |
* smarty; also check writeablility of the templates_c directory. A static
|
| 13 |
* variable is used so we only do the checks once.
|
| 14 |
*/
|
| 15 |
function _smarty_init() {
|
| 16 |
static $ok;
|
| 17 |
|
| 18 |
if ( isset($ok) ) {
|
| 19 |
return $ok;
|
| 20 |
}
|
| 21 |
|
| 22 |
$doclink = t(' Please refer to the documentation on the <a href="http://www.drupal.org/project/smarty">smarty project page</a>.');
|
| 23 |
|
| 24 |
// phptemplate functions are inherited wherever possible
|
| 25 |
$phptemplate_path = './themes/engines/phptemplate/phptemplate.engine';
|
| 26 |
if ( file_exists($phptemplate_path) ) {
|
| 27 |
include_once $phptemplate_path;
|
| 28 |
} else {
|
| 29 |
drupal_set_message(t('smarty could not find phptemplate.engine for inclusion.').$doclink, 'error');
|
| 30 |
$ok = FALSE;
|
| 31 |
}
|
| 32 |
|
| 33 |
// find the Smarty class
|
| 34 |
if (!class_exists('Smarty')) { // prevent redeclaration
|
| 35 |
$smarty_path = dirname(__FILE__).'/smarty/libs/Smarty.class.php';
|
| 36 |
@include_once $smarty_path;
|
| 37 |
if ( !class_exists('Smarty') ) {
|
| 38 |
drupal_set_message(t('smarty could not find Smarty.class.php for inclusion.').$doclink, 'error');
|
| 39 |
$ok = FALSE;
|
| 40 |
}
|
| 41 |
}
|
| 42 |
|
| 43 |
$compile_dir = dirname(__FILE__).'/templates_c';
|
| 44 |
|
| 45 |
if ( !is_writeable($compile_dir) ) {
|
| 46 |
drupal_set_message(t('smarty cannot write to the compiled template cache.').$doclink, 'error');
|
| 47 |
$ok = FALSE;
|
| 48 |
}
|
| 49 |
if ( $ok!==FALSE ) {
|
| 50 |
$ok = TRUE;
|
| 51 |
}
|
| 52 |
return $ok;
|
| 53 |
}
|
| 54 |
|
| 55 |
function smarty_init($template) {
|
| 56 |
_smarty_init();
|
| 57 |
return phptemplate_init($template);
|
| 58 |
}
|
| 59 |
|
| 60 |
function smarty_templates($directory = 'themes') {
|
| 61 |
// only return smarty themes if there is no problem with initialisation
|
| 62 |
if ( _smarty_init() ) {
|
| 63 |
return drupal_system_listing('^page\.tpl$', $directory, 'filename');
|
| 64 |
} else {
|
| 65 |
return array();
|
| 66 |
}
|
| 67 |
}
|
| 68 |
|
| 69 |
function smarty_regions() {
|
| 70 |
return phptemplate_regions();
|
| 71 |
}
|
| 72 |
|
| 73 |
// function _smarty_callback($hook, $variables = array(), $suggestions = array()) {
|
| 74 |
// return _phptemplate_callback($hook, $variables, $suggestions);
|
| 75 |
// }
|
| 76 |
|
| 77 |
// function _smarty_default_variables($hook, $variables) { }
|
| 78 |
|
| 79 |
function smarty_features() {
|
| 80 |
return phptemplate_features();
|
| 81 |
}
|
| 82 |
|
| 83 |
function smarty_page($content, $show_blocks = TRUE) {
|
| 84 |
return phptemplate_page($content, $show_blocks);
|
| 85 |
}
|
| 86 |
|
| 87 |
function smarty_node($node, $teaser = 0, $page = 0) {
|
| 88 |
return phptemplate_node($node, $teaser, $page);
|
| 89 |
}
|
| 90 |
|
| 91 |
function smarty_comment($comment, $links = 0) {
|
| 92 |
return phptemplate_comment($comment, $links);
|
| 93 |
}
|
| 94 |
|
| 95 |
function smarty_block($block) {
|
| 96 |
return phptemplate_block($block);
|
| 97 |
}
|
| 98 |
|
| 99 |
function smarty_box($title, $content, $region = 'main') {
|
| 100 |
return phptemplate_box($title, $content, $region);
|
| 101 |
}
|
| 102 |
|
| 103 |
// This is the only chance to add in some global theme variables (should be implemented in PHPtemplate)
|
| 104 |
// Also use the default of .tpl instead of .tpl.php
|
| 105 |
function _smarty_default($hook, $variables, $suggestions = array(), $extension = '.tpl') {
|
| 106 |
$variables['user'] = $GLOBALS['user'];
|
| 107 |
$variables['base_path'] = base_path();
|
| 108 |
$variables['path_to_theme'] = path_to_theme();
|
| 109 |
return _phptemplate_default($hook, $variables, $suggestions, $extension);
|
| 110 |
}
|
| 111 |
|
| 112 |
function &smarty_get_object() {
|
| 113 |
static $smarty;
|
| 114 |
|
| 115 |
if ( !isset($smarty) ) {
|
| 116 |
$smarty = new Smarty;
|
| 117 |
$smarty->template_dir = realpath('./');
|
| 118 |
$smarty->compile_dir = variable_get('smarty_compile_dir', dirname(__FILE__).'/templates_c');
|
| 119 |
$smarty->compile_check = variable_get('smarty_compile_check', TRUE);
|
| 120 |
// $smarty->config_dir = $path.'/configs';
|
| 121 |
// $smarty->plugins_dir = array(realpath('themes/engines/smarty/plugins'), $path.'/plugins', 'plugins');
|
| 122 |
$smarty->register_function('theme', 'smarty_function_theme');
|
| 123 |
$smarty->register_function('theme_links', 'smarty_function_theme_links');
|
| 124 |
}
|
| 125 |
return $smarty;
|
| 126 |
}
|
| 127 |
|
| 128 |
function _smarty_render($file, $variables) {
|
| 129 |
|
| 130 |
$smarty = &smarty_get_object();
|
| 131 |
$smarty->_tpl_vars = $variables; // REVISIT could use clear and assign but this is quicker and simpler
|
| 132 |
$contents = $smarty->fetch($file);
|
| 133 |
return $contents;
|
| 134 |
}
|
| 135 |
|
| 136 |
/**
|
| 137 |
* Smarty theme function plugin
|
| 138 |
* This implements a call to the Drupal theme() function within a Smarty template.
|
| 139 |
*/
|
| 140 |
function smarty_function_theme($params, &$smarty) {
|
| 141 |
$function = array_shift($params);
|
| 142 |
if ($func = theme_get_function($function)) {
|
| 143 |
return call_user_func_array($func, $params);
|
| 144 |
}
|
| 145 |
}
|
| 146 |
|
| 147 |
/**
|
| 148 |
* Smarty theme_links function plugin
|
| 149 |
* Return themed HTML for links
|
| 150 |
*/
|
| 151 |
function smarty_function_theme_links($params, &$smarty) {
|
| 152 |
if ( isset($params['links']) ) {
|
| 153 |
if ( !isset($params['class']) ) {
|
| 154 |
$params['class'] = 'links';
|
| 155 |
}
|
| 156 |
if ( isset($params['id']) ) {
|
| 157 |
$attribs = array('class' =>$params['class'], 'id' => $params['id']);
|
| 158 |
} else {
|
| 159 |
$attribs = array('class' =>$params['class']);
|
| 160 |
}
|
| 161 |
return theme('links', $params['links'], $attribs);
|
| 162 |
} else {
|
| 163 |
return '';
|
| 164 |
}
|
| 165 |
}
|
| 166 |
|