From 416270ee77c32d9ff876dedc4b6c4a2eb7296a96 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sat, 22 Nov 2008 23:53:40 +0000 Subject: [PATCH] - Deprecate S_ROW_COUNT use S_ROW_NUM - S_ROW_NUM, S_FIRST_ROW, S_LAST_ROW are now using internal template engine variables saving memory - Other small changes - Update of template tests git-svn-id: file:///svn/phpbb/trunk@9087 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/functions_template.php | 58 ++++++++++++++++++++++--- phpBB/includes/template.php | 49 +-------------------- tests/template/template.php | 24 ++++++++-- tests/template/templates/loop.html | 4 ++ tests/template/templates/loop_vars.html | 2 + 5 files changed, 78 insertions(+), 59 deletions(-) diff --git a/phpBB/includes/functions_template.php b/phpBB/includes/functions_template.php index cf85927028..d13ffc04a4 100644 --- a/phpBB/includes/functions_template.php +++ b/phpBB/includes/functions_template.php @@ -413,7 +413,34 @@ class template_filter extends php_user_filter $varrefs = array(); if (preg_match('#^((?:[a-z0-9\-_]+\.)+)?(\$)?(?=[A-Z])([A-Z0-9\-_]+)#s', $token, $varrefs)) { - $token = (!empty($varrefs[1])) ? $this->generate_block_data_ref(substr($varrefs[1], 0, -1), true, $varrefs[2]) . '[\'' . $varrefs[3] . '\']' : (($varrefs[2]) ? '$_tpldata[\'DEFINE\'][\'.\'][\'' . $varrefs[3] . '\']' : '$_rootref[\'' . $varrefs[3] . '\']'); + if (!empty($varrefs[1])) + { + $namespace = substr($varrefs[1], 0, -1); + $namespace = (strpos($namespace, '.') === false) ? $namespace : strrchr($namespace, '.'); + + // S_ROW_COUNT is deceptive, it returns the current row number now the number of rows + // hence S_ROW_COUNT is deprecated in favour of S_ROW_NUM + if ($varrefs[3] == 'S_ROW_NUM' || $varrefs[3] == 'S_ROW_COUNT') + { + $token = "\$_${namespace}_i"; + } + else if ($varrefs[3] == 'S_FIRST_ROW') + { + $token = "(\$_${namespace}_i == 0)"; + } + else if ($varrefs[3] == 'S_LAST_ROW') + { + $token = "(\$_${namespace}_i == \$_${namespace}_count - 1)"; + } + else + { + $token = $this->generate_block_data_ref(substr($varrefs[1], 0, -1), true, $varrefs[2]) . '[\'' . $varrefs[3] . '\']'; + } + } + else + { + $token = ($varrefs[2]) ? '$_tpldata[\'DEFINE\'][\'.\'][\'' . $varrefs[3] . '\']' : '$_rootref[\'' . $varrefs[3] . '\']'; + } } else if (preg_match('#^\.((?:[a-z0-9\-_]+\.?)+)$#s', $token, $varrefs)) { @@ -439,7 +466,7 @@ class template_filter extends php_user_filter // Add the block reference for the last child. $varref .= "['" . $blocks[0] . "']"; } - $token = "sizeof($varref)"; + $token = "isset($varref) && sizeof($varref)"; } break; @@ -579,12 +606,29 @@ class template_filter extends php_user_filter // Strip the trailing period. $namespace = substr($namespace, 0, -1); - // Get a reference to the data block for this namespace. - $varref = $this->generate_block_data_ref($namespace, true, $defop); - // Prepend the necessary code to stick this in an echo line. + // S_ROW_COUNT is deceptive, it returns the current row number now the number of rows + // hence S_ROW_COUNT is deprecated in favour of S_ROW_NUM + if ($varname == 'S_ROW_NUM' || $varname == 'S_ROW_COUNT') + { + $varref = "\$_${namespace}_i"; + } + else if ($varname == 'S_FIRST_ROW') + { + $varref = "(\$_${namespace}_i == 0)"; + } + else if ($varname == 'S_LAST_ROW') + { + $varref = "(\$_${namespace}_i == \$_${namespace}_count - 1)"; + } + else + { + // Get a reference to the data block for this namespace. + $varref = $this->generate_block_data_ref($namespace, true, $defop); + // Prepend the necessary code to stick this in an echo line. - // Append the variable reference. - $varref .= "['$varname']"; + // Append the variable reference. + $varref .= "['$varname']"; + } $varref = ($echo) ? "" : ((isset($varref)) ? $varref : ''); return $varref; diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index 814d6d2e50..25f2fa1cad 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -211,7 +211,7 @@ class template $filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . PHP_EXT; - $recompile = (($config['load_tplcompile'] && @filemtime($filename) < filemtime($this->files[$handle])) || !file_exists($filename) || @filesize($filename) === 0) ? true : false; + $recompile = (!file_exists($filename) || @filesize($filename) === 0 || ($config['load_tplcompile'] && @filemtime($filename) < filemtime($this->files[$handle]))) ? true : false; // Recompile page if the original template is newer, otherwise load the compiled version if ($recompile) @@ -311,23 +311,6 @@ class template $str = &$str[sizeof($str) - 1]; } - $s_row_count = isset($str[$blocks[$blockcount]]) ? sizeof($str[$blocks[$blockcount]]) : 0; - $vararray['S_ROW_COUNT'] = $s_row_count; - - // Assign S_FIRST_ROW - if (!$s_row_count) - { - $vararray['S_FIRST_ROW'] = true; - } - - // Now the tricky part, we always assign S_LAST_ROW and remove the entry before - // This is much more clever than going through the complete template data on display (phew) - $vararray['S_LAST_ROW'] = true; - if ($s_row_count > 0) - { - unset($str[$blocks[$blockcount]][($s_row_count - 1)]['S_LAST_ROW']); - } - // Now we add the block that we're actually assigning to. // We're adding a new iteration to this block with the given // variable assignments. @@ -336,21 +319,6 @@ class template else { // Top-level block. - $s_row_count = (isset($this->_tpldata[$blockname])) ? sizeof($this->_tpldata[$blockname]) : 0; - $vararray['S_ROW_COUNT'] = $s_row_count; - - // Assign S_FIRST_ROW - if (!$s_row_count) - { - $vararray['S_FIRST_ROW'] = true; - } - - // We always assign S_LAST_ROW and remove the entry before - $vararray['S_LAST_ROW'] = true; - if ($s_row_count > 0) - { - unset($this->_tpldata[$blockname][($s_row_count - 1)]['S_LAST_ROW']); - } // Add a new iteration to this block with the variable assignments we were given. $this->_tpldata[$blockname][] = $vararray; @@ -427,28 +395,13 @@ class template // Insert Block if ($mode == 'insert') { - // Make sure we are not exceeding the last iteration - if ($key >= sizeof($this->_tpldata[$blockname])) - { - $key = sizeof($this->_tpldata[$blockname]); - unset($this->_tpldata[$blockname][($key - 1)]['S_LAST_ROW']); - $vararray['S_LAST_ROW'] = true; - } - else if ($key === 0) - { - unset($this->_tpldata[$blockname][0]['S_FIRST_ROW']); - $vararray['S_FIRST_ROW'] = true; - } - // Re-position template blocks for ($i = sizeof($this->_tpldata[$blockname]); $i > $key; $i--) { $this->_tpldata[$blockname][$i] = $this->_tpldata[$blockname][$i-1]; - $this->_tpldata[$blockname][$i]['S_ROW_COUNT'] = $i; } // Insert vararray at given position - $vararray['S_ROW_COUNT'] = $key; $this->_tpldata[$blockname][$key] = $vararray; return true; diff --git a/tests/template/template.php b/tests/template/template.php index e2d9429511..0639e263bb 100644 --- a/tests/template/template.php +++ b/tests/template/template.php @@ -44,9 +44,25 @@ class phpbb_template_template_test extends PHPUnit_Framework_TestCase protected function setUp() { + // Test the engine can be used + $this->setup_engine(); + if (!is_writable(dirname($this->template->cachepath))) + { + $this->markTestSkipped("Template cache directory is not writable."); + } + + $this->error_reporting = error_reporting(error_reporting() & ~E_NOTICE); } + protected function tearDown() + { + error_reporting($this->error_reporting); + } + + /** + * @todo put test data into templates/xyz.test + */ public static function template_data() { return array( @@ -92,19 +108,19 @@ class phpbb_template_template_test extends PHPUnit_Framework_TestCase 'loop.html', array(), array('loop' => array(array(), array())), - "loop\nloop\nloop", + "loop\nloop\nloop\nloop", ), array( 'loop_vars.html', array(), array('loop' => array(array('VARIABLE' => 'x'))), - "first\n0\nx\nlast", + "first\n0\n0\nx\nlast", ), array( 'loop_vars.html', array(), array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y'))), - "first\n0\nx\n1\ny\nlast", + "first\n0\n0\nx\n1\n1\ny\nlast", ), array( 'define.html', @@ -138,7 +154,7 @@ class phpbb_template_template_test extends PHPUnit_Framework_TestCase } } - $this->assertEquals($expected, $this->display('test'), "Testing $file.html"); + $this->assertEquals($expected, $this->display('test'), "Testing $file"); } } ?> \ No newline at end of file diff --git a/tests/template/templates/loop.html b/tests/template/templates/loop.html index ae921fc929..3912635e9d 100644 --- a/tests/template/templates/loop.html +++ b/tests/template/templates/loop.html @@ -9,3 +9,7 @@ loop noloop + + +loop + diff --git a/tests/template/templates/loop_vars.html b/tests/template/templates/loop_vars.html index 0da0af4758..c2924ae18f 100644 --- a/tests/template/templates/loop_vars.html +++ b/tests/template/templates/loop_vars.html @@ -3,6 +3,8 @@ {loop.S_ROW_COUNT} +{loop.S_ROW_NUM} + {loop.VARIABLE} last