Better handling of template data. It's a class member now, so multiple template objects can exist at the same time without sharing data. yippee.

git-svn-id: file:///svn/phpbb/trunk@523 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
natec 2001-06-24 21:36:21 +00:00
parent 50871fb159
commit 82fc9744ec

View file

@ -28,24 +28,18 @@
* *
*/ */
// global variable that holds all the data we'll be substituting into
// the compiled templates.
// This will end up being a multi-dimensional array like this:
// $_tpldata[block.][iteration#][child.][iteration#][child2.][iteration#][variablename] == value
// if it's a root-level variable, it'll be like this:
// $_tpldata[.][0][varname] == value
if (!isset($_tpldata))
{
$_tpldata = array();
}
class Template { class Template {
var $classname = "Template"; var $classname = "Template";
// variable that holds all the data we'll be substituting into
// the compiled templates.
// ...
// This will end up being a multi-dimensional array like this:
// $this->_tpldata[block.][iteration#][child.][iteration#][child2.][iteration#][variablename] == value
// 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. // Hash of filenames for each template handle.
var $files = array(); var $files = array();
@ -73,8 +67,7 @@ class Template {
*/ */
function destroy() function destroy()
{ {
global $_tpldata; $this->_tpldata = array();
$_tpldata = array();
} }
/** /**
@ -119,8 +112,6 @@ class Template {
*/ */
function pparse($handle) function pparse($handle)
{ {
global $_tpldata;
if (!$this->loadfile($handle)) 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");
@ -149,7 +140,6 @@ class Template {
*/ */
function assign_var_from_handle($varname, $handle) function assign_var_from_handle($varname, $handle)
{ {
global $_tpldata;
if (!$this->loadfile($handle)) 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");
@ -176,14 +166,12 @@ class Template {
*/ */
function assign_block_vars($blockname, $vararray) function assign_block_vars($blockname, $vararray)
{ {
global $_tpldata;
if (strstr($blockname, '.')) if (strstr($blockname, '.'))
{ {
// Nested block. // Nested block.
$blocks = explode('.', $blockname); $blocks = explode('.', $blockname);
$blockcount = sizeof($blocks) - 1; $blockcount = sizeof($blocks) - 1;
$str = '$_tpldata'; $str = '$this->_tpldata';
for ($i = 0; $i < $blockcount; $i++) for ($i = 0; $i < $blockcount; $i++)
{ {
$str .= '[\'' . $blocks[$i] . '.\']'; $str .= '[\'' . $blocks[$i] . '.\']';
@ -203,7 +191,7 @@ class Template {
// Top-level block. // Top-level block.
// Add a new iteration to this block with the variable assignments // Add a new iteration to this block with the variable assignments
// we were given. // we were given.
$_tpldata[$blockname . '.'][] = $vararray; $this->_tpldata[$blockname . '.'][] = $vararray;
} }
return true; return true;
@ -215,12 +203,10 @@ class Template {
*/ */
function assign_vars($vararray) function assign_vars($vararray)
{ {
global $_tpldata;
reset ($vararray); reset ($vararray);
while (list($key, $val) = each($vararray)) while (list($key, $val) = each($vararray))
{ {
$_tpldata['.'][0][$key] = $val; $this->_tpldata['.'][0][$key] = $val;
} }
return true; return true;
@ -232,9 +218,7 @@ class Template {
*/ */
function assign_var($varname, $varval) function assign_var($varname, $varval)
{ {
global $_tpldata; $this->_tpldata['.'][0][$varname] = $varval;
$_tpldata['.'][0][$varname] = $varval;
return true; return true;
} }
@ -268,8 +252,6 @@ class Template {
*/ */
function loadfile($handle) function loadfile($handle)
{ {
global $_tpldata;
// If the file for this handle is already loaded and compiled, do nothing. // If the file for this handle is already loaded and compiled, do nothing.
if (isset($this->uncompiled_code[$handle]) && !empty($this->uncompiled_code[$handle])) if (isset($this->uncompiled_code[$handle]) && !empty($this->uncompiled_code[$handle]))
{ {
@ -306,8 +288,6 @@ class Template {
*/ */
function compile($code, $do_not_echo = false) function compile($code, $do_not_echo = false)
{ {
global $_tpldata;
// replace \ with \\ and then ' with \'. // replace \ with \\ and then ' with \'.
$code = str_replace('\\', '\\\\', $code); $code = str_replace('\\', '\\\\', $code);
$code = str_replace('\'', '\\\'', $code); $code = str_replace('\'', '\\\'', $code);
@ -328,7 +308,7 @@ class Template {
} }
// This will handle the remaining root-level varrefs // This will handle the remaining root-level varrefs
$code = preg_replace('#\{([a-z0-9\-_]*?)\}#is', '\' . $_tpldata[\'.\'][0][\'\1\'] . \'', $code); $code = preg_replace('#\{([a-z0-9\-_]*?)\}#is', '\' . $this->_tpldata[\'.\'][0][\'\1\'] . \'', $code);
// Break it up into lines. // Break it up into lines.
$code_lines = explode("\n", $code); $code_lines = explode("\n", $code);
@ -351,7 +331,7 @@ class Template {
if ($block_nesting_level < 2) if ($block_nesting_level < 2)
{ {
// Block is not nested. // Block is not nested.
$code_lines[$i] = '$_' . $m[1] . '_count = sizeof($_tpldata[\'' . $m[1] . '.\']);'; $code_lines[$i] = '$_' . $m[1] . '_count = sizeof($this->_tpldata[\'' . $m[1] . '.\']);';
$code_lines[$i] .= "\n" . 'for ($_' . $m[1] . '_i = 0; $_' . $m[1] . '_i < $_' . $m[1] . '_count; $_' . $m[1] . '_i++)'; $code_lines[$i] .= "\n" . 'for ($_' . $m[1] . '_i = 0; $_' . $m[1] . '_i < $_' . $m[1] . '_count; $_' . $m[1] . '_i++)';
$code_lines[$i] .= "\n" . '{'; $code_lines[$i] .= "\n" . '{';
} }
@ -401,7 +381,7 @@ class Template {
/** /**
* Generates a reference to the given variable inside the given (possibly nested) * Generates a reference to the given variable inside the given (possibly nested)
* block namespace. This is a string of the form: * block namespace. This is a string of the form:
* ' . $_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['varname'] . ' * ' . $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['varname'] . '
* It's ready to be inserted into an "echo" line in one of the templates. * It's ready to be inserted into an "echo" line in one of the templates.
* NOTE: expects a trailing "." on the namespace. * NOTE: expects a trailing "." on the namespace.
*/ */
@ -425,7 +405,7 @@ class Template {
/** /**
* 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: * (possibly nested) block namespace. This is a string of the form:
* $_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['$childN'] * $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['$childN']
* *
* If $include_last_iterator is true, then [$_childN_i] will be appended to the form shown above. * If $include_last_iterator is true, then [$_childN_i] will be appended to the form shown above.
* NOTE: does not expect a trailing "." on the blockname. * NOTE: does not expect a trailing "." on the blockname.
@ -435,7 +415,7 @@ class Template {
// Get an array of the blocks involved. // Get an array of the blocks involved.
$blocks = explode(".", $blockname); $blocks = explode(".", $blockname);
$blockcount = sizeof($blocks) - 1; $blockcount = sizeof($blocks) - 1;
$varref = '$_tpldata'; $varref = '$this->_tpldata';
// Build up the string with everything but the last child. // Build up the string with everything but the last child.
for ($i = 0; $i < $blockcount; $i++) for ($i = 0; $i < $blockcount; $i++)
{ {