[ Index ] |
|
Code source de Serendipity 1.2 |
1 <?php # $Id: plugin_api.inc.php 1228 2006-06-01 11:18:53Z garvinhicking $ 2 # Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team) 3 # All rights reserved. See LICENSE file for licensing details 4 5 /* 6 *@author Garvin Hicking 7 *@state EXPERIMENTAL 8 9 This file provides a basic Smarty emulating layer 10 You can use different template engines than the default Smarty one 11 by putting a "template.inc.php" file into your template directory. 12 It should look something like this: 13 14 <?php 15 include_once S9Y_INCLUDE_PATH . 'include/template_api.inc.php'; 16 $GLOBALS['template'] = new serendipity_smarty_emulator(); 17 $GLOBALS['serendipity']['smarty'] =& $GLOBALS['template']; 18 ?> 19 20 You could of course also use inherited different template classes. It is important 21 that your own template object contains method declarations like the class below 22 for full interoperability to Serendipity templates. It is important that 23 you assign a reference copy of your template object to the $serendipity['smarty'] 24 object for backwards compatibility. 25 26 All variables that are assigned from this class to your templates/.php files 27 will be put into $GLOBALS['tpl']. 28 29 Since the scope of includes can vary, you'll need to use $GLOBALS['tpl'] instead 30 of just $tpl in some cases. Thus it's recommended to always use the $GLOBALS['tpl'] 31 way. Also it's safer to use $GLOBALS['serendipity'] / $GLOBALS['template'] in most 32 cases because of the same reason. 33 34 Instead of Smarty $CONST.xxx constants you can use the usual 'xxx' constant access 35 method by PHP. 36 37 You can use any Smarty template file to construct your custom PHP template. You 38 just need to do this: 39 40 - Replace '{$variable}' calls with '<?= $GLOBALS['tpl']['variable'] ?>'. 41 42 - Replace '{$variable|XXX}' smarty modifiers with corresponding PHP code, like: 43 '<?= substr($GLOBALS['tpl']['XXX'], 0, 25) ?>' 44 would correspond with 45 '{$variable|truncate:'...':25}' 46 - Replace '{if CONDITION} ... {/if}' checks with '<?php if (CONDITION): ?> ... <?php endif; ?>' 47 48 - Replace '{foreach} ... {/foreach}' calls correspondingly. 49 50 - Replace '{smartycommand param1=x param2=x}' function calls with 51 '<?php $GLOBALS['template']->call('smartycommand', array('param1' => 'x', 'param2' => 'x')); ?>' ones 52 53 - NOTA BENE: Be aware that many smarty function calls are just wrappers to Serendipity API 54 calls. To save grandma's performance pennies you should search the original Serendipity API 55 function before calling them with the $GLOBALS['template']->call() wrapper! This costs dearly. 56 57 The Serendipity Admin backend will still make use of Smarty. It rocks. 58 59 Know your PHP before you think about using this. :-) 60 */ 61 62 class serendipity_smarty_emulator { 63 var $compile_dir = '/tmp'; // Not used 64 var $security_settings = array(); // Not used 65 66 /** 67 * Assign one or multiple template variable 68 * 69 * @param mixed Either a variable name, or an array of variables 70 * @param mixed Either null or the variable content. 71 * @access public 72 * @return null 73 */ 74 function assign($tpl_var, $value = null) { 75 if (is_array($tpl_var)) { 76 foreach ($tpl_var as $key => $val) { 77 if ($key != '') { 78 $GLOBALS['tpl'][$key] = $tpl_var[$key]; 79 } 80 } 81 } else { 82 $GLOBALS['tpl'][$tpl_var] = $value; 83 } 84 85 return true; 86 } 87 88 /** 89 * Assign one or multiple template variable by reference 90 * 91 * @param string Variable name 92 * @param mixed Referenced variable 93 * @access public 94 * @return null 95 */ 96 function assign_by_ref($tpl_var, &$value) { 97 $GLOBALS['tpl'][$tpl_var] =& $value; 98 99 return true; 100 } 101 102 /** 103 * Helper function to call a 'serendipity_smarty_xx' function with less parameters. 104 * 105 * @param string Function name to call. 106 * @param array Array of parameters 107 * @access public 108 * @return string Output 109 */ 110 function call($funcname, $params) { 111 if (function_exists('serendipity_smarty_' . $funcname)) { 112 return call_user_func('serendipity_smarty_' . $funcname, $params, $this); 113 } elseif (function_exists('serendipity_' . $funcname)) { 114 return call_user_func('serendipity_' . $funcname, $params, $this); 115 } elseif (function_exists($funcname)) { 116 return call_user_func($funcname, $params, $this); 117 } else { 118 return "ERROR: " . htmlspecialchars($funcname) . " NOT FOUND.<br />\n"; 119 } 120 } 121 122 /** 123 * Outputs a smarty template. 124 * 125 * @param string Full path to template file 126 * @access public 127 * @return boolean 128 */ 129 function display($resource_name) { 130 return include $resource_name; 131 } 132 133 /** 134 * Triggers a template error 135 * 136 * @param string Error message 137 * @access public 138 * @return null 139 */ 140 function trigger_error($txt) { 141 echo '<b>SMARTY EMULATOR ERROR: ' . $txt; 142 } 143 144 /** 145 * Echoes a default value. Append multiple values and will output the first non empty value. 146 * 147 * @param mixed The value to emit. 148 * @access public 149 * @return null 150 */ 151 function getdefault() { 152 $vars = func_get_args(); 153 foreach($vars as $title) { 154 if (!empty($GLOBALS['tpl'][$title])) { 155 return $GLOBALS['tpl'][$title]; 156 } 157 } 158 159 return false; 160 } 161 162 /** 163 * Parses a template file into another. 164 * 165 * @param string The path to the resource name (prefixed with 'file:' usually) 166 * @param string The Cache ID (not used) 167 * @param string The Compile ID (not used) 168 * @param boolean Output data (true) or return it (false)? 169 * @access public 170 * @return null 171 */ 172 function &fetch($resource_name, $cache_id = null, $compile_id = null, $display = false) { 173 $resource_name = str_replace('file:', '', $resource_name); 174 175 if (!$display) { 176 ob_start(); 177 } 178 179 include $resource_name; 180 181 if (!$display) { 182 $out = ob_get_contents(); 183 ob_end_clean(); 184 return $out; 185 } else { 186 return true; 187 } 188 } 189 } 190 191 /* 192 *@author Garvin Hicking 193 *@state EXPERIMENTAL 194 * 195 * XML Engine 196 */ 197 198 class serendipity_smarty_emulator_xml extends serendipity_smarty_emulator { 199 /** 200 * Parses a template file into another. 201 * 202 * @access public 203 * @return null 204 */ 205 function fetch() { 206 return true; 207 } 208 209 /** 210 * Outputs a smarty template. 211 * 212 * @access public 213 * @return null 214 */ 215 function display() { 216 echo "</serendipity>\n"; 217 return true; 218 } 219 220 function __construct() { 221 header('Content-Type: text/xml; charset=' . LANG_CHARSET); 222 echo '<?xml version="1.0" encoding="' . LANG_CHARSET . '" ?>' . "\n"; 223 /* 224 echo '<?xml-stylesheet href="' . serendipity_getTemplateFile('xml.css') . '" type="text/css" ?>' . "\n"; 225 */ 226 echo "<serendipity>\n"; 227 ob_end_flush(); // This ends the started ob from index.php! 228 } 229 230 function serendipity_smarty_emulator_xml() { 231 $this->__construct(); 232 } 233 234 /** 235 * Assign one or multiple template variable 236 * @TODO: Why can't this function accept references. This sucks. 237 * 238 * @param mixed Either a variable name, or an array of variables 239 * @param mixed Either null or the variable content. 240 * @access public 241 * @return null 242 */ 243 function assign($tpl_var, $value = null, $level = 0) { 244 if (is_array($tpl_var)) { 245 foreach ($tpl_var as $key => $val) { 246 $this->createXML($level, $key, $val); 247 } 248 } else { 249 $this->createXML($level, $tpl_var, $value); 250 } 251 252 return true; 253 } 254 255 /** 256 * Assign one or multiple template variable by reference 257 * 258 * @param string Variable name 259 * @param mixed Referenced variable 260 * @access public 261 * @return null 262 */ 263 function assign_by_ref($tpl_var, &$value) { 264 if (is_array($value)) { 265 foreach ($value as $key => $val) { 266 $this->createXML($level, $key, $val); 267 } 268 } else { 269 $this->createXML($level, $tpl_var, $value); 270 } 271 272 return true; 273 } 274 275 /** 276 * Create the XML output for an element 277 * 278 * @param int The intend level 279 * @param mixed The XML element name 280 * @param mixed The XML element value 281 */ 282 function createXML(&$level, &$key, &$val) { 283 if (is_numeric($key)) { 284 $openkey = 'item index="' . $key . '"'; 285 $closekey = 'item'; 286 } else { 287 $openkey = $closekey = str_replace(':', '_', $key); 288 } 289 290 if (is_array($val)) { 291 echo str_repeat("\t", $level) . "<$openkey>\n"; 292 $this->assign($val, null, $level + 1); 293 echo str_repeat("\t", $level) . "</$closekey>\n"; 294 } else { 295 echo str_repeat("\t", $level) . "<$openkey>" . htmlspecialchars($val) . "</$closekey>\n"; 296 } 297 } 298 } 299
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Nov 24 09:00:37 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |