diff options
Diffstat (limited to 'phpBB/includes/template.php')
-rw-r--r-- | phpBB/includes/template.php | 191 |
1 files changed, 100 insertions, 91 deletions
diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 105dcb5867..b043749721 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -1,21 +1,30 @@ <?php -/*************************************************************************** +/*************************************************************************** * template.inc - * ------------------- - * begin : Saturday, Feb 13, 2001 - * copyright : (C) 2001 The phpBB Group - * email : support@phpbb.com - * + * ------------------- + * begin : Saturday, Feb 13, 2001 + * copyright : (C) 2001 The phpBB Group + * email : support@phpbb.com + * * $Id$ - * - * - ***************************************************************************/ + * + * + ***************************************************************************/ + +/*************************************************************************** + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + ***************************************************************************/ /** * Template class. By Nathan Codding of the phpBB group. * The interface was originally inspired by PHPLib templates, * and the template file formats are quite similar. - * + * */ class Template { @@ -29,24 +38,24 @@ class Template { // if it's a root-level variable, it'll be like this: // $this->_tpldata[.][0][varname] == value var $_tpldata = array(); - + // Hash of filenames for each template handle. var $files = array(); // Root template directory. var $root = ""; - + // this will hash handle names to the compiled code for that handle. var $compiled_code = array(); - + // This will hold the uncompiled code for that handle. var $uncompiled_code = array(); - + /** * Constructor. Simply sets the root dir. - * + * */ - function Template($root = ".") + function Template($root = ".") { $this->set_rootdir($root); } @@ -62,39 +71,39 @@ class Template { /** * Sets the template root directory for this Template object. - */ - function set_rootdir($dir) + */ + function set_rootdir($dir) { - if (!is_dir($dir)) + if (!is_dir($dir)) { return false; } - + $this->root = $dir; return true; } - /** + /** * Sets the template filenames for handles. $filename_array * should be a hash of handle => filename pairs. */ - function set_filenames($filename_array) + function set_filenames($filename_array) { if (!is_array($filename_array)) { - return false; + return false; } - + reset($filename_array); - while(list($handle, $filename) = each($filename_array)) + while(list($handle, $filename) = each($filename_array)) { $this->files[$handle] = $this->make_filename($filename); } - + return true; } - + /** * Load the file for the handle, compile the file, * and run the compiled code. This will print out @@ -104,26 +113,26 @@ class Template { { if (!$this->loadfile($handle)) { - die("Template->pparse(): Couldn't load template file for handle $handle"); + die("Template->pparse(): Couldn't load template file for handle $handle"); } - + // actually compile the template now. if (!isset($this->compiled_code[$handle]) || empty($this->compiled_code[$handle])) { // Actually compile the code now. $this->compiled_code[$handle] = $this->compile($this->uncompiled_code[$handle]); } - + // Run the compiled code. eval($this->compiled_code[$handle]); return true; } - + /** * Inserts the uncompiled code for $handle as the * value of $varname in the root-level. This can be used - * to effectively include a template in the middle of another + * to effectively include a template in the middle of another * template. * Note that all desired assignments to the variables in $handle should be done * BEFORE calling this function. @@ -132,33 +141,33 @@ class Template { { if (!$this->loadfile($handle)) { - die("Template->assign_var_from_handle(): Couldn't load template file for handle $handle"); + die("Template->assign_var_from_handle(): Couldn't load template file for handle $handle"); } - + // Compile it, with the "no echo statements" option on. $code = $this->compile($this->uncompiled_code[$handle], true); // turn it into a variable assignment. $code = '$_str = \'' . $code . '\';'; - + // evaluate the variable assignment. eval($code); - + // assign the value of the generated variable to the given varname. $this->assign_var($varname, $_str); - + return true; } - + /** * Block-level variable assignment. Adds a new block iteration with the given - * variable assignments. Note that this should only be called once per block + * variable assignments. Note that this should only be called once per block * iteration. */ function assign_block_vars($blockname, $vararray) { if (strstr($blockname, '.')) { - // Nested block. + // Nested block. $blocks = explode('.', $blockname); $blockcount = sizeof($blocks) - 1; $str = '$this->_tpldata'; @@ -172,7 +181,7 @@ class Template { // We're adding a new iteration to this block with the given // variable assignments. $str .= '[\'' . $blocks[$blockcount] . '.\'][] = $vararray;'; - + // Now we evaluate this assignment we've built up. eval($str); } @@ -183,10 +192,10 @@ class Template { // we were given. $this->_tpldata[$blockname . '.'][] = $vararray; } - + return true; } - + /** * Root-level variable assignment. Adds to current assignments, overriding * any existing variable assignment with the same name. @@ -196,12 +205,12 @@ class Template { reset ($vararray); while (list($key, $val) = each($vararray)) { - $this->_tpldata['.'][0][$key] = $val; + $this->_tpldata['.'][0][$key] = $val; } return true; } - + /** * Root-level variable assignment. Adds to current assignments, overriding * any existing variable assignment with the same name. @@ -209,38 +218,38 @@ class Template { function assign_var($varname, $varval) { $this->_tpldata['.'][0][$varname] = $varval; - - return true; + + return true; } - - - /** + + + /** * Generates a full path+filename for the given filename, which can either * be an absolute name, or a name relative to the rootdir for this Template * object. */ - function make_filename($filename) + function make_filename($filename) { // Check if it's an absolute or relative path. if (substr($filename, 0, 1) != '/') { $filename = $this->root . '/' . $filename; } - + if (!file_exists($filename)) { die("Template->make_filename(): Error - file $filename does not exist"); } - + return $filename; } - - + + /** * If not already done, load the file for the given handle and populate * the uncompiled_code[] hash with its code. Do not compile. */ - function loadfile($handle) + function loadfile($handle) { // If the file for this handle is already loaded and compiled, do nothing. if (isset($this->uncompiled_code[$handle]) && !empty($this->uncompiled_code[$handle])) @@ -249,31 +258,31 @@ class Template { } // If we don't have a file assigned to this handle, die. - if (!isset($this->files[$handle])) + if (!isset($this->files[$handle])) { die("Template->loadfile(): No file specified for handle $handle"); } - + $filename = $this->files[$handle]; $str = implode("", @file($filename)); - if (empty($str)) + if (empty($str)) { die("Template->loadfile(): File $filename for handle $handle is empty"); } - + $this->uncompiled_code[$handle] = $str; - + return true; } - - - + + + /** * Compiles the given string of code, and returns * the result in a string. * If "do_not_echo" is true, the returned code will not be directly - * executable, but can be used as part of a variable assignment + * executable, but can be used as part of a variable assignment * for use in assign_code_from_handle(). */ function compile($code, $do_not_echo = false) @@ -281,9 +290,9 @@ class Template { // replace \ with \\ and then ' with \'. $code = str_replace('\\', '\\\\', $code); $code = str_replace('\'', '\\\'', $code); - + // change template varrefs into PHP varrefs - + // This one will handle varrefs WITH namespaces $varrefs = array(); preg_match_all('#\{(([a-z0-9\-_]+?\.)+?)([a-z0-9\-_]+?)\}#is', $code, $varrefs); @@ -293,20 +302,20 @@ class Template { $namespace = $varrefs[1][$i]; $varname = $varrefs[3][$i]; $new = $this->generate_block_varref($namespace, $varname); - + $code = str_replace($varrefs[0][$i], $new, $code); } - + // This will handle the remaining root-level varrefs $code = preg_replace('#\{([a-z0-9\-_]*?)\}#is', '\' . $this->_tpldata[\'.\'][0][\'\1\'] . \'', $code); - + // Break it up into lines. $code_lines = explode("\n", $code); - + $block_nesting_level = 0; $block_names = array(); $block_names[0] = "."; - + // Second: prepend echo ', append ' . "\n"; to each line. $line_count = sizeof($code_lines); for ($i = 0; $i < $line_count; $i++) @@ -332,12 +341,12 @@ class Template { else { // This block is nested. - + // Generate a namespace string for this block. $namespace = implode('.', $block_names); // strip leading period from root level.. $namespace = substr($namespace, 2); - // Get a reference to the data array for this block that depends on the + // Get a reference to the data array for this block that depends on the // current indices of all parent blocks. $varref = $this->generate_block_data_ref($namespace, false); // Create the for loop code to iterate over this block. @@ -345,7 +354,7 @@ class Template { $code_lines[$i] .= "\n" . 'for ($_' . $n[1] . '_i = 0; $_' . $n[1] . '_i < $_' . $n[1] . '_count; $_' . $n[1] . '_i++)'; $code_lines[$i] .= "\n" . '{'; } - + // We have the end of a block. unset($block_names[$block_nesting_level]); $block_nesting_level--; @@ -368,19 +377,19 @@ class Template { else { // This block is nested. - + // Generate a namespace string for this block. $namespace = implode('.', $block_names); // strip leading period from root level.. $namespace = substr($namespace, 2); - // Get a reference to the data array for this block that depends on the + // Get a reference to the data array for this block that depends on the // current indices of all parent blocks. $varref = $this->generate_block_data_ref($namespace, false); // Create the for loop code to iterate over this block. $code_lines[$i] = '$_' . $m[1] . '_count = sizeof(' . $varref . ');'; $code_lines[$i] .= "\n" . 'for ($_' . $m[1] . '_i = 0; $_' . $m[1] . '_i < $_' . $m[1] . '_count; $_' . $m[1] . '_i++)'; $code_lines[$i] .= "\n" . '{'; - } + } } } else if (preg_match('#<!-- END (.*?) -->#', $code_lines[$i], $m)) @@ -399,14 +408,14 @@ class Template { } } } - + // Bring it back into a single string of lines of code. - $code = implode("\n", $code_lines); + $code = implode("\n", $code_lines); return $code ; - + } - - + + /** * Generates a reference to the given variable inside the given (possibly nested) * block namespace. This is a string of the form: @@ -418,21 +427,21 @@ class Template { { // Strip the trailing period. $namespace = substr($namespace, 0, strlen($namespace) - 1); - + // Get a reference to the data block for this namespace. $varref = $this->generate_block_data_ref($namespace, true); // Prepend the necessary code to stick this in an echo line. $varref = '\' . ' . $varref; // Append the variable reference. $varref .= '[\'' . $varname . '\'] . \''; - + return $varref; - + } - - + + /** - * Generates a reference to the array of data values for the given + * Generates a reference to the array of data values for the given * (possibly nested) block namespace. This is a string of the form: * $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['$childN'] * @@ -448,7 +457,7 @@ class Template { // Build up the string with everything but the last child. for ($i = 0; $i < $blockcount; $i++) { - $varref .= '[\'' . $blocks[$i] . '.\'][$_' . $blocks[$i] . '_i]'; + $varref .= '[\'' . $blocks[$i] . '.\'][$_' . $blocks[$i] . '_i]'; } // Add the block reference for the last child. $varref .= '[\'' . $blocks[$blockcount] . '.\']'; @@ -457,7 +466,7 @@ class Template { { $varref .= '[$_' . $blocks[$blockcount] . '_i]'; } - + return $varref; } |