[351] | 1 | <?php |
---|
[362] | 2 | // +-----------------------------------------------------------------------+ |
---|
[593] | 3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
| 4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
[1719] | 5 | // | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net | |
---|
[362] | 6 | // +-----------------------------------------------------------------------+ |
---|
[593] | 7 | // | branch : BSF (Best So Far) |
---|
[1719] | 8 | // | file : $Id: template.php 1900 2007-03-12 22:33:53Z rub $ |
---|
[362] | 9 | // | last update : $Date: 2007-03-12 22:33:53 +0000 (Mon, 12 Mar 2007) $ |
---|
| 10 | // | last modifier : $Author: rub $ |
---|
| 11 | // | revision : $Revision: 1900 $ |
---|
| 12 | // +-----------------------------------------------------------------------+ |
---|
| 13 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 14 | // | it under the terms of the GNU General Public License as published by | |
---|
| 15 | // | the Free Software Foundation | |
---|
| 16 | // | | |
---|
| 17 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 20 | // | General Public License for more details. | |
---|
| 21 | // | | |
---|
| 22 | // | You should have received a copy of the GNU General Public License | |
---|
| 23 | // | along with this program; if not, write to the Free Software | |
---|
| 24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 25 | // | USA. | |
---|
| 26 | // +-----------------------------------------------------------------------+ |
---|
[351] | 27 | |
---|
| 28 | /** |
---|
[402] | 29 | * Template class. By Nathan Codding of the phpBB group. The interface was |
---|
| 30 | * originally inspired by PHPLib templates, and the template file formats |
---|
| 31 | * are quite similar. |
---|
[351] | 32 | */ |
---|
| 33 | |
---|
| 34 | class Template { |
---|
| 35 | |
---|
[402] | 36 | var $classname = "Template"; |
---|
[1092] | 37 | |
---|
[402] | 38 | // variable that holds all the data we'll be substituting into |
---|
| 39 | // the compiled templates. |
---|
| 40 | // ... |
---|
| 41 | // This will end up being a multi-dimensional array like this : |
---|
| 42 | // $this->_tpldata[block.][iteration#][child.][iteration#][child2.][iteration#][variablename] == value |
---|
| 43 | // if it's a root-level variable, it'll be like this: |
---|
| 44 | // $this->_tpldata[.][0][varname] == value |
---|
| 45 | var $_tpldata = array(); |
---|
[1092] | 46 | |
---|
[402] | 47 | // Hash of filenames for each template handle. |
---|
| 48 | var $files = array(); |
---|
[1092] | 49 | |
---|
[402] | 50 | // Root template directory. |
---|
| 51 | var $root = ""; |
---|
[351] | 52 | |
---|
[402] | 53 | // this will hash handle names to the compiled code for that handle. |
---|
| 54 | var $compiled_code = array(); |
---|
[1092] | 55 | |
---|
[402] | 56 | // This will hold the uncompiled code for that handle. |
---|
| 57 | var $uncompiled_code = array(); |
---|
[687] | 58 | |
---|
| 59 | // output |
---|
| 60 | var $output = ''; |
---|
[1092] | 61 | |
---|
[1568] | 62 | var $themeconf = array(); |
---|
| 63 | |
---|
[402] | 64 | /** |
---|
| 65 | * Constructor. Simply sets the root dir. |
---|
| 66 | * |
---|
| 67 | */ |
---|
[1568] | 68 | function Template($root = ".", $theme= "") |
---|
[402] | 69 | { |
---|
[1568] | 70 | if ( $this->set_rootdir($root) ) |
---|
| 71 | { |
---|
| 72 | if ( !empty( $theme ) ) |
---|
| 73 | { |
---|
| 74 | include($root.'/theme/'.$theme.'/themeconf.inc.php'); |
---|
| 75 | $this->themeconf = $themeconf; |
---|
| 76 | } |
---|
| 77 | } |
---|
[402] | 78 | } |
---|
[1092] | 79 | |
---|
[402] | 80 | /** |
---|
| 81 | * Destroys this template object. Should be called when you're done with |
---|
| 82 | * it, in order to clear out the template data so you can load/parse a new |
---|
| 83 | * template set. |
---|
| 84 | */ |
---|
| 85 | function destroy() |
---|
| 86 | { |
---|
| 87 | $this->_tpldata = array(); |
---|
| 88 | } |
---|
[351] | 89 | |
---|
[402] | 90 | /** |
---|
| 91 | * Sets the template root directory for this Template object. |
---|
| 92 | */ |
---|
| 93 | function set_rootdir($dir) |
---|
| 94 | { |
---|
| 95 | if (!is_dir($dir)) |
---|
| 96 | { |
---|
| 97 | return false; |
---|
| 98 | } |
---|
[1092] | 99 | |
---|
[402] | 100 | $this->root = $dir; |
---|
| 101 | return true; |
---|
| 102 | } |
---|
[1092] | 103 | |
---|
[402] | 104 | /** |
---|
[1753] | 105 | * Sets the template filename for handle. |
---|
| 106 | */ |
---|
| 107 | function set_filename($handle, $filename) |
---|
| 108 | { |
---|
| 109 | return $this->set_filenames( array($handle=>$filename) ); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
[402] | 113 | * Sets the template filenames for handles. $filename_array should be a |
---|
| 114 | * hash of handle => filename pairs. |
---|
| 115 | */ |
---|
| 116 | function set_filenames($filename_array) |
---|
| 117 | { |
---|
| 118 | if (!is_array($filename_array)) |
---|
| 119 | { |
---|
| 120 | return false; |
---|
| 121 | } |
---|
[1092] | 122 | |
---|
[402] | 123 | reset($filename_array); |
---|
| 124 | while(list($handle, $filename) = each($filename_array)) |
---|
| 125 | { |
---|
[1753] | 126 | if (is_null($filename)) |
---|
| 127 | { |
---|
| 128 | unset( $this->files[$handle] ); |
---|
| 129 | } |
---|
| 130 | else |
---|
| 131 | { |
---|
| 132 | $this->files[$handle] = $this->make_filename($filename); |
---|
| 133 | } |
---|
| 134 | unset($this->compiled_code[$handle]); |
---|
| 135 | unset($this->uncompiled_code[$handle]); |
---|
[402] | 136 | } |
---|
[1092] | 137 | |
---|
[402] | 138 | return true; |
---|
| 139 | } |
---|
[1092] | 140 | |
---|
| 141 | |
---|
[402] | 142 | /** |
---|
| 143 | * Load the file for the handle, compile the file, and run the compiled |
---|
| 144 | * code. This will print out the results of executing the template. |
---|
| 145 | */ |
---|
| 146 | function pparse($handle) |
---|
| 147 | { |
---|
| 148 | if (!$this->loadfile($handle)) |
---|
| 149 | { |
---|
| 150 | die("Template->pparse(): Couldn't load template file for handle $handle"); |
---|
| 151 | } |
---|
[1092] | 152 | |
---|
[402] | 153 | // actually compile the template now. |
---|
| 154 | if (!isset($this->compiled_code[$handle]) || empty($this->compiled_code[$handle])) |
---|
| 155 | { |
---|
| 156 | // Actually compile the code now. |
---|
| 157 | $this->compiled_code[$handle] = $this->compile($this->uncompiled_code[$handle]); |
---|
| 158 | } |
---|
[351] | 159 | |
---|
[402] | 160 | // Run the compiled code. |
---|
| 161 | //echo ("<!-- ".$this->compiled_code[$handle]." -->"); |
---|
| 162 | eval($this->compiled_code[$handle]); |
---|
| 163 | return true; |
---|
| 164 | } |
---|
[687] | 165 | |
---|
| 166 | /** |
---|
[1589] | 167 | * fills $output template var by default or returns the content |
---|
[687] | 168 | */ |
---|
[1589] | 169 | function parse($handle, $return=false) |
---|
[687] | 170 | { |
---|
| 171 | if (!$this->loadfile($handle)) |
---|
| 172 | { |
---|
| 173 | die("Template->pparse(): Couldn't load template file for handle $handle"); |
---|
| 174 | } |
---|
[1092] | 175 | |
---|
[687] | 176 | // actually compile the template now. |
---|
| 177 | if (!isset($this->compiled_code[$handle]) || empty($this->compiled_code[$handle])) |
---|
| 178 | { |
---|
| 179 | // Actually compile the code now. |
---|
| 180 | $this->compiled_code[$handle] = $this->compile($this->uncompiled_code[$handle], true, '_str'); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | // Run the compiled code. |
---|
| 184 | $_str = ''; |
---|
| 185 | eval($this->compiled_code[$handle]); |
---|
[1589] | 186 | if ($return) |
---|
| 187 | { |
---|
| 188 | return $_str; |
---|
| 189 | } |
---|
[687] | 190 | $this->output.= $_str; |
---|
| 191 | |
---|
| 192 | return true; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | /** |
---|
| 196 | * prints $output template var |
---|
| 197 | */ |
---|
| 198 | function p() |
---|
| 199 | { |
---|
| 200 | echo $this->output; |
---|
| 201 | } |
---|
[1092] | 202 | |
---|
[402] | 203 | /** |
---|
| 204 | * Inserts the uncompiled code for $handle as the value of $varname in the |
---|
| 205 | * root-level. This can be used to effectively include a template in the |
---|
| 206 | * middle of another template. |
---|
| 207 | * |
---|
| 208 | * Note that all desired assignments to the variables in $handle should be |
---|
| 209 | * done BEFORE calling this function. |
---|
| 210 | */ |
---|
| 211 | function assign_var_from_handle($varname, $handle) |
---|
| 212 | { |
---|
| 213 | if (!$this->loadfile($handle)) |
---|
| 214 | { |
---|
| 215 | die("Template->assign_var_from_handle(): Couldn't load template file for handle $handle"); |
---|
| 216 | } |
---|
[1092] | 217 | |
---|
[402] | 218 | // Compile it, with the "no echo statements" option on. |
---|
| 219 | $_str = ""; |
---|
| 220 | $code = $this->compile($this->uncompiled_code[$handle], true, '_str'); |
---|
[1092] | 221 | |
---|
[402] | 222 | // evaluate the variable assignment. |
---|
| 223 | eval($code); |
---|
| 224 | // assign the value of the generated variable to the given varname. |
---|
| 225 | $this->assign_var($varname, $_str); |
---|
[1092] | 226 | |
---|
[402] | 227 | return true; |
---|
| 228 | } |
---|
[1092] | 229 | |
---|
[402] | 230 | /** |
---|
| 231 | * Block-level variable assignment. Adds a new block iteration with the |
---|
| 232 | * given variable assignments. Note that this should only be called once |
---|
| 233 | * per block iteration. |
---|
| 234 | */ |
---|
| 235 | function assign_block_vars($blockname, $vararray) |
---|
| 236 | { |
---|
| 237 | if (strstr($blockname, '.')) |
---|
| 238 | { |
---|
| 239 | // Nested block. |
---|
| 240 | $blocks = explode('.', $blockname); |
---|
| 241 | $blockcount = sizeof($blocks) - 1; |
---|
| 242 | $str = '$this->_tpldata'; |
---|
| 243 | for ($i = 0; $i < $blockcount; $i++) |
---|
| 244 | { |
---|
| 245 | $str .= '[\'' . $blocks[$i] . '.\']'; |
---|
[1579] | 246 | eval('$lastiteration = isset('.$str.') ? sizeof('.$str.')-1:0;'); |
---|
[402] | 247 | $str .= '[' . $lastiteration . ']'; |
---|
| 248 | } |
---|
| 249 | // Now we add the block that we're actually assigning to. |
---|
| 250 | // We're adding a new iteration to this block with the given |
---|
| 251 | // variable assignments. |
---|
| 252 | $str .= '[\'' . $blocks[$blockcount] . '.\'][] = $vararray;'; |
---|
[1092] | 253 | |
---|
[402] | 254 | // Now we evaluate this assignment we've built up. |
---|
| 255 | eval($str); |
---|
| 256 | } |
---|
| 257 | else |
---|
| 258 | { |
---|
| 259 | // Top-level block. Add a new iteration to this block with the |
---|
| 260 | // variable assignments we were given. |
---|
| 261 | $this->_tpldata[$blockname . '.'][] = $vararray; |
---|
| 262 | } |
---|
[1092] | 263 | |
---|
[402] | 264 | return true; |
---|
| 265 | } |
---|
[1092] | 266 | |
---|
[402] | 267 | /** |
---|
[1587] | 268 | * Block-level variable merge. Merge given variables to the last block |
---|
| 269 | * iteration. This can be called several times per block iteration. |
---|
| 270 | */ |
---|
| 271 | function merge_block_vars($blockname, $vararray) |
---|
| 272 | { |
---|
| 273 | $blocks = explode('.', $blockname); |
---|
| 274 | $blockcount = count($blocks); |
---|
| 275 | $str = '$this->_tpldata'; |
---|
| 276 | for ($i = 0; $i < $blockcount; $i++) |
---|
| 277 | { |
---|
| 278 | $str .= '[\'' . $blocks[$i] . '.\']'; |
---|
| 279 | eval('$lastiteration = isset('.$str.') ? sizeof('.$str.')-1:-1;'); |
---|
| 280 | if ($lastiteration==-1) |
---|
| 281 | { |
---|
| 282 | return false; |
---|
| 283 | } |
---|
| 284 | $str .= '[' . $lastiteration . ']'; |
---|
| 285 | } |
---|
| 286 | $str = $str.'=array_merge('.$str.', $vararray);'; |
---|
| 287 | eval($str); |
---|
| 288 | return true; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | /** |
---|
[402] | 292 | * Root-level variable assignment. Adds to current assignments, overriding |
---|
| 293 | * any existing variable assignment with the same name. |
---|
| 294 | */ |
---|
| 295 | function assign_vars($vararray) |
---|
| 296 | { |
---|
| 297 | reset ($vararray); |
---|
| 298 | while (list($key, $val) = each($vararray)) |
---|
| 299 | { |
---|
| 300 | $this->_tpldata['.'][0][$key] = $val; |
---|
| 301 | } |
---|
[1092] | 302 | |
---|
[402] | 303 | return true; |
---|
| 304 | } |
---|
[1092] | 305 | |
---|
[402] | 306 | /** |
---|
| 307 | * Root-level variable assignment. Adds to current assignments, overriding |
---|
| 308 | * any existing variable assignment with the same name. |
---|
| 309 | */ |
---|
| 310 | function assign_var($varname, $varval) |
---|
| 311 | { |
---|
| 312 | $this->_tpldata['.'][0][$varname] = $varval; |
---|
[1719] | 313 | return true; |
---|
| 314 | } |
---|
[1092] | 315 | |
---|
[1719] | 316 | /** |
---|
| 317 | * Root-level variable concatenation. Appends a string to an existing |
---|
| 318 | * variable assignment with the same name. |
---|
| 319 | */ |
---|
| 320 | function concat_var($varname, $varval) |
---|
| 321 | { |
---|
| 322 | if ( isset($this->_tpldata['.'][0][$varname]) ) |
---|
| 323 | { |
---|
| 324 | $this->_tpldata['.'][0][$varname] .= $varval; |
---|
| 325 | } |
---|
| 326 | else |
---|
| 327 | { |
---|
| 328 | $this->_tpldata['.'][0][$varname] = $varval; |
---|
| 329 | } |
---|
[402] | 330 | return true; |
---|
| 331 | } |
---|
[1092] | 332 | |
---|
[1719] | 333 | /** |
---|
| 334 | * Returns a root-level variable value |
---|
| 335 | */ |
---|
| 336 | function get_var($varname, $default=null) |
---|
| 337 | { |
---|
| 338 | if ( isset($this->_tpldata['.'][0][$varname]) ) |
---|
| 339 | { |
---|
| 340 | return $this->_tpldata['.'][0][$varname]; |
---|
| 341 | } |
---|
| 342 | return $default; |
---|
| 343 | } |
---|
[1092] | 344 | |
---|
[402] | 345 | /** |
---|
| 346 | * Generates a full path+filename for the given filename, which can either |
---|
| 347 | * be an absolute name, or a name relative to the rootdir for this |
---|
| 348 | * Template object. |
---|
| 349 | */ |
---|
| 350 | function make_filename($filename) |
---|
| 351 | { |
---|
| 352 | // Check if it's an absolute or relative path. |
---|
[1589] | 353 | // if (substr($filename, 0, 1) != '/') |
---|
| 354 | if (preg_match('/^[a-z_][^:]/i', $filename) ) |
---|
[402] | 355 | { |
---|
[749] | 356 | $filename = $this->root.'/'.$filename; |
---|
[402] | 357 | } |
---|
[1092] | 358 | |
---|
[402] | 359 | if (!file_exists($filename)) |
---|
| 360 | { |
---|
| 361 | die("Template->make_filename(): Error - file $filename does not exist"); |
---|
| 362 | } |
---|
[589] | 363 | |
---|
[402] | 364 | return $filename; |
---|
| 365 | } |
---|
[1092] | 366 | |
---|
| 367 | |
---|
[402] | 368 | /** |
---|
| 369 | * If not already done, load the file for the given handle and populate |
---|
| 370 | * the uncompiled_code[] hash with its code. Do not compile. |
---|
| 371 | */ |
---|
| 372 | function loadfile($handle) |
---|
| 373 | { |
---|
| 374 | // If the file for this handle is already loaded and compiled, do |
---|
| 375 | // nothing. |
---|
| 376 | if (isset($this->uncompiled_code[$handle]) |
---|
| 377 | and !empty($this->uncompiled_code[$handle])) |
---|
| 378 | { |
---|
| 379 | return true; |
---|
| 380 | } |
---|
[1092] | 381 | |
---|
[402] | 382 | // If we don't have a file assigned to this handle, die. |
---|
| 383 | if (!isset($this->files[$handle])) |
---|
| 384 | { |
---|
| 385 | die("Template->loadfile(): No file specified for handle $handle"); |
---|
| 386 | } |
---|
[1092] | 387 | |
---|
[402] | 388 | $filename = $this->files[$handle]; |
---|
[1092] | 389 | |
---|
[402] | 390 | $str = implode("", @file($filename)); |
---|
[1579] | 391 | |
---|
[402] | 392 | if (empty($str)) |
---|
| 393 | { |
---|
| 394 | die("Template->loadfile(): File $filename for handle $handle is empty"); |
---|
| 395 | } |
---|
[1568] | 396 | |
---|
[402] | 397 | $this->uncompiled_code[$handle] = $str; |
---|
[1568] | 398 | |
---|
[402] | 399 | return true; |
---|
| 400 | } |
---|
[1568] | 401 | |
---|
| 402 | |
---|
| 403 | |
---|
[402] | 404 | /** |
---|
| 405 | * Compiles the given string of code, and returns the result in a string. |
---|
| 406 | * |
---|
| 407 | * If "do_not_echo" is true, the returned code will not be directly |
---|
| 408 | * executable, but can be used as part of a variable assignment for use in |
---|
| 409 | * assign_code_from_handle(). |
---|
| 410 | */ |
---|
| 411 | function compile($code, $do_not_echo = false, $retvar = '') |
---|
| 412 | { |
---|
[795] | 413 | // PWG specific : communication between template and $lang |
---|
| 414 | $code = preg_replace('/\{lang:([^}]+)\}/e', "l10n('$1')", $code); |
---|
[960] | 415 | // PWG specific : expand themeconf.inc.php variables |
---|
[1568] | 416 | $code = preg_replace('/\{themeconf:([^}]+)\}/e', '$this->get_themeconf(\'$1\')', $code); |
---|
[1456] | 417 | $code = preg_replace('/\{pwg_root\}/e', "get_root_url()", $code); |
---|
[1092] | 418 | |
---|
[402] | 419 | // replace \ with \\ and then ' with \'. |
---|
| 420 | $code = str_replace('\\', '\\\\', $code); |
---|
| 421 | $code = str_replace('\'', '\\\'', $code); |
---|
[1092] | 422 | |
---|
[402] | 423 | // change template varrefs into PHP varrefs |
---|
[1092] | 424 | |
---|
[402] | 425 | // This one will handle varrefs WITH namespaces |
---|
| 426 | $varrefs = array(); |
---|
| 427 | preg_match_all('#\{(([a-z0-9\-_]+?\.)+?)([a-z0-9\-_]+?)\}#is', $code, $varrefs); |
---|
| 428 | $varcount = sizeof($varrefs[1]); |
---|
| 429 | for ($i = 0; $i < $varcount; $i++) |
---|
| 430 | { |
---|
| 431 | $namespace = $varrefs[1][$i]; |
---|
| 432 | $varname = $varrefs[3][$i]; |
---|
| 433 | $new = $this->generate_block_varref($namespace, $varname); |
---|
[1092] | 434 | |
---|
[402] | 435 | $code = str_replace($varrefs[0][$i], $new, $code); |
---|
| 436 | } |
---|
[1092] | 437 | |
---|
[402] | 438 | // This will handle the remaining root-level varrefs |
---|
| 439 | $code = preg_replace('#\{([a-z0-9\-_]*?)\}#is', '\' . ( ( isset($this->_tpldata[\'.\'][0][\'\1\']) ) ? $this->_tpldata[\'.\'][0][\'\1\'] : \'\' ) . \'', $code); |
---|
[1092] | 440 | |
---|
[402] | 441 | // Break it up into lines. |
---|
| 442 | $code_lines = explode("\n", $code); |
---|
[1092] | 443 | |
---|
[402] | 444 | $block_nesting_level = 0; |
---|
| 445 | $block_names = array(); |
---|
| 446 | $block_names[0] = "."; |
---|
[1092] | 447 | |
---|
[402] | 448 | // Second: prepend echo ', append ' . "\n"; to each line. |
---|
| 449 | $line_count = sizeof($code_lines); |
---|
| 450 | for ($i = 0; $i < $line_count; $i++) |
---|
| 451 | { |
---|
| 452 | $code_lines[$i] = chop($code_lines[$i]); |
---|
| 453 | if (preg_match('#<!-- BEGIN (.*?) -->#', $code_lines[$i], $m)) |
---|
| 454 | { |
---|
| 455 | $n[0] = $m[0]; |
---|
| 456 | $n[1] = $m[1]; |
---|
[1092] | 457 | |
---|
[402] | 458 | // Added: dougk_ff7-Keeps templates from bombing if begin is on |
---|
| 459 | // the same line as end.. I think. :) |
---|
| 460 | if ( preg_match('#<!-- END (.*?) -->#', $code_lines[$i], $n) ) |
---|
| 461 | { |
---|
| 462 | $block_nesting_level++; |
---|
| 463 | $block_names[$block_nesting_level] = $m[1]; |
---|
| 464 | if ($block_nesting_level < 2) |
---|
| 465 | { |
---|
| 466 | // Block is not nested. |
---|
| 467 | $code_lines[$i] = '$_' . $n[1] . '_count = ( isset($this->_tpldata[\'' . $n[1] . '.\']) ) ? sizeof($this->_tpldata[\'' . $n[1] . '.\']) : 0;'; |
---|
| 468 | $code_lines[$i] .= "\n" . 'for ($_' . $n[1] . '_i = 0; $_' . $n[1] . '_i < $_' . $n[1] . '_count; $_' . $n[1] . '_i++)'; |
---|
| 469 | $code_lines[$i] .= "\n" . '{'; |
---|
| 470 | } |
---|
| 471 | else |
---|
| 472 | { |
---|
| 473 | // This block is nested. |
---|
[1092] | 474 | |
---|
[402] | 475 | // Generate a namespace string for this block. |
---|
| 476 | $namespace = implode('.', $block_names); |
---|
| 477 | // strip leading period from root level.. |
---|
| 478 | $namespace = substr($namespace, 2); |
---|
| 479 | // Get a reference to the data array for this block that depends on the |
---|
| 480 | // current indices of all parent blocks. |
---|
| 481 | $varref = $this->generate_block_data_ref($namespace, false); |
---|
| 482 | // Create the for loop code to iterate over this block. |
---|
| 483 | $code_lines[$i] = '$_' . $n[1] . '_count = ( isset(' . $varref . ') ) ? sizeof(' . $varref . ') : 0;'; |
---|
| 484 | $code_lines[$i] .= "\n" . 'for ($_' . $n[1] . '_i = 0; $_' . $n[1] . '_i < $_' . $n[1] . '_count; $_' . $n[1] . '_i++)'; |
---|
| 485 | $code_lines[$i] .= "\n" . '{'; |
---|
| 486 | } |
---|
[1092] | 487 | |
---|
[402] | 488 | // We have the end of a block. |
---|
| 489 | unset($block_names[$block_nesting_level]); |
---|
| 490 | $block_nesting_level--; |
---|
| 491 | $code_lines[$i] .= '} // END ' . $n[1]; |
---|
| 492 | $m[0] = $n[0]; |
---|
| 493 | $m[1] = $n[1]; |
---|
| 494 | } |
---|
| 495 | else |
---|
| 496 | { |
---|
| 497 | // We have the start of a block. |
---|
| 498 | $block_nesting_level++; |
---|
| 499 | $block_names[$block_nesting_level] = $m[1]; |
---|
| 500 | if ($block_nesting_level < 2) |
---|
| 501 | { |
---|
| 502 | // Block is not nested. |
---|
| 503 | $code_lines[$i] = '$_' . $m[1] . '_count = ( isset($this->_tpldata[\'' . $m[1] . '.\']) ) ? sizeof($this->_tpldata[\'' . $m[1] . '.\']) : 0;'; |
---|
| 504 | $code_lines[$i] .= "\n" . 'for ($_' . $m[1] . '_i = 0; $_' . $m[1] . '_i < $_' . $m[1] . '_count; $_' . $m[1] . '_i++)'; |
---|
| 505 | $code_lines[$i] .= "\n" . '{'; |
---|
| 506 | } |
---|
| 507 | else |
---|
| 508 | { |
---|
| 509 | // This block is nested. |
---|
[1092] | 510 | |
---|
[402] | 511 | // Generate a namespace string for this block. |
---|
| 512 | $namespace = implode('.', $block_names); |
---|
| 513 | // strip leading period from root level.. |
---|
| 514 | $namespace = substr($namespace, 2); |
---|
| 515 | // Get a reference to the data array for this block that |
---|
| 516 | // depends on the current indices of all parent blocks. |
---|
| 517 | $varref = $this->generate_block_data_ref($namespace, false); |
---|
| 518 | // Create the for loop code to iterate over this block. |
---|
| 519 | $code_lines[$i] = '$_' . $m[1] . '_count = ( isset(' . $varref . ') ) ? sizeof(' . $varref . ') : 0;'; |
---|
| 520 | $code_lines[$i] .= "\n" . 'for ($_' . $m[1] . '_i = 0; $_' . $m[1] . '_i < $_' . $m[1] . '_count; $_' . $m[1] . '_i++)'; |
---|
| 521 | $code_lines[$i] .= "\n" . '{'; |
---|
| 522 | } |
---|
| 523 | } |
---|
| 524 | } |
---|
| 525 | else if (preg_match('#<!-- END (.*?) -->#', $code_lines[$i], $m)) |
---|
| 526 | { |
---|
| 527 | // We have the end of a block. |
---|
| 528 | unset($block_names[$block_nesting_level]); |
---|
| 529 | $block_nesting_level--; |
---|
| 530 | $code_lines[$i] = '} // END ' . $m[1]; |
---|
| 531 | } |
---|
| 532 | else |
---|
| 533 | { |
---|
| 534 | // We have an ordinary line of code. |
---|
| 535 | if (!$do_not_echo) |
---|
| 536 | { |
---|
| 537 | $code_lines[$i] = 'echo \'' . $code_lines[$i] . '\' . "\\n";'; |
---|
| 538 | } |
---|
| 539 | else |
---|
| 540 | { |
---|
[1092] | 541 | $code_lines[$i] = '$' . $retvar . '.= \'' . $code_lines[$i] . '\' . "\\n";'; |
---|
[402] | 542 | } |
---|
| 543 | } |
---|
| 544 | } |
---|
[1092] | 545 | |
---|
[402] | 546 | // Bring it back into a single string of lines of code. |
---|
| 547 | $code = implode("\n", $code_lines); |
---|
| 548 | return $code ; |
---|
[1092] | 549 | |
---|
[402] | 550 | } |
---|
[1092] | 551 | |
---|
| 552 | |
---|
[402] | 553 | /** |
---|
| 554 | * Generates a reference to the given variable inside the given (possibly |
---|
| 555 | * nested) block namespace. This is a string of the form: ' |
---|
| 556 | * . $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['varname'] |
---|
| 557 | * . ' It's ready to be inserted into an "echo" line in one of the |
---|
| 558 | * templates. NOTE: expects a trailing "." on the namespace. |
---|
| 559 | */ |
---|
| 560 | function generate_block_varref($namespace, $varname) |
---|
| 561 | { |
---|
| 562 | // Strip the trailing period. |
---|
| 563 | $namespace = substr($namespace, 0, strlen($namespace) - 1); |
---|
[1092] | 564 | |
---|
[402] | 565 | // Get a reference to the data block for this namespace. |
---|
| 566 | $varref = $this->generate_block_data_ref($namespace, true); |
---|
| 567 | // Prepend the necessary code to stick this in an echo line. |
---|
[1092] | 568 | |
---|
[402] | 569 | // Append the variable reference. |
---|
| 570 | $varref .= '[\'' . $varname . '\']'; |
---|
[1092] | 571 | |
---|
[402] | 572 | $varref = '\' . ( ( isset(' . $varref . ') ) ? ' . $varref . ' : \'\' ) . \''; |
---|
[1092] | 573 | |
---|
[402] | 574 | return $varref; |
---|
[1092] | 575 | |
---|
[402] | 576 | } |
---|
[1092] | 577 | |
---|
| 578 | |
---|
[402] | 579 | /** |
---|
| 580 | * Generates a reference to the array of data values for the given |
---|
| 581 | * (possibly nested) block namespace. This is a string of the form: |
---|
| 582 | * $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['$childN'] |
---|
| 583 | * |
---|
| 584 | * If $include_last_iterator is true, then [$_childN_i] will be appended |
---|
| 585 | * to the form shown above. NOTE: does not expect a trailing "." on the |
---|
| 586 | * blockname. |
---|
| 587 | */ |
---|
| 588 | function generate_block_data_ref($blockname, $include_last_iterator) |
---|
| 589 | { |
---|
| 590 | // Get an array of the blocks involved. |
---|
| 591 | $blocks = explode(".", $blockname); |
---|
| 592 | $blockcount = sizeof($blocks) - 1; |
---|
| 593 | $varref = '$this->_tpldata'; |
---|
| 594 | // Build up the string with everything but the last child. |
---|
| 595 | for ($i = 0; $i < $blockcount; $i++) |
---|
| 596 | { |
---|
| 597 | $varref .= '[\'' . $blocks[$i] . '.\'][$_' . $blocks[$i] . '_i]'; |
---|
| 598 | } |
---|
| 599 | // Add the block reference for the last child. |
---|
| 600 | $varref .= '[\'' . $blocks[$blockcount] . '.\']'; |
---|
| 601 | // Add the iterator for the last child if requried. |
---|
| 602 | if ($include_last_iterator) |
---|
| 603 | { |
---|
| 604 | $varref .= '[$_' . $blocks[$blockcount] . '_i]'; |
---|
| 605 | } |
---|
[1092] | 606 | |
---|
[402] | 607 | return $varref; |
---|
| 608 | } |
---|
[1092] | 609 | |
---|
[1568] | 610 | function get_themeconf($key) |
---|
| 611 | { |
---|
| 612 | return isset($this->themeconf[$key]) ? $this->themeconf[$key] : ''; |
---|
| 613 | } |
---|
[351] | 614 | } |
---|
| 615 | |
---|
| 616 | ?> |
---|