1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-30 21:40:43 +02:00

- Add template variable S_BLOCK_NAME

- Modify template::alter_block_array() so it supports modification of nested blocks
- Add (incomplete) tests for template::alter_block_array()


git-svn-id: file:///svn/phpbb/trunk@9116 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Chris Smith
2008-11-25 00:31:32 +00:00
parent 332521a369
commit c95f0c7935
4 changed files with 322 additions and 10 deletions

View File

@@ -458,6 +458,10 @@ class template_filter extends php_user_filter
$token = "(\$_${namespace}_i == \$_${namespace}_count - 1)";
break;
case 'S_BLOCK_NAME':
$token = "'$namespace'";
break;
default:
$token = $this->generate_block_data_ref(substr($varrefs[1], 0, -1), true, $varrefs[2]) . '[\'' . $varrefs[3] . '\']';
break;
@@ -637,6 +641,8 @@ class template_filter extends php_user_filter
// Strip the trailing period.
$namespace = substr($namespace, 0, -1);
$expr = true;
// 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
switch ($varname)
@@ -658,6 +664,10 @@ class template_filter extends php_user_filter
$varref = "(\$_${namespace}_i == \$_${namespace}_count - 1)";
break;
case 'S_BLOCK_NAME':
$varref = "'$namespace'";
break;
default:
// Get a reference to the data block for this namespace.
$varref = $this->generate_block_data_ref($namespace, true, $defop);
@@ -665,9 +675,12 @@ class template_filter extends php_user_filter
// Append the variable reference.
$varref .= "['$varname']";
$expr = false;
break;
}
$varref = ($echo) ? "<?php echo $varref; ?>" : ((isset($varref)) ? $varref : '');
// @todo Test the !$expr more
$varref = ($echo) ? "<?php echo $varref; ?>" : (($expr || isset($varref)) ? $varref : '');
return $varref;
}

View File

@@ -386,14 +386,47 @@ class template
{
if (strpos($blockname, '.') !== false)
{
// Nested blocks are not supported
return false;
// Nested block.
$blocks = explode('.', $blockname);
$blockcount = sizeof($blocks) - 1;
$block = &$this->_tpldata;
for ($i = 0; $i < $blockcount; $i++)
{
if (($pos = strpos($blocks[$i], '[')) !== false)
{
$name = substr($blocks[$i], 0, $pos);
if (strpos($blocks[$i], '[]') === $pos)
{
$index = sizeof($block[$name]) - 1;
}
else
{
$index = min((int) substr($blocks[$i], $pos + 1, -1), sizeof($block[$name]) - 1);
}
}
else
{
$name = $blocks[$i];
$index = sizeof($block[$name]) - 1;
}
$block = &$block[$name];
$block = &$block[$index];
}
$block = &$block[$blocks[$i]]; // Traverse the last block
}
else
{
// Top-level block.
$block = &$this->_tpldata[$blockname];
}
// Change key to zero (change first position) if false and to last position if true
if ($key === false || $key === true)
{
$key = ($key === false) ? 0 : sizeof($this->_tpldata[$blockname]);
$key = ($key === false) ? 0 : sizeof($block);
}
// Get correct position if array given
@@ -403,7 +436,7 @@ class template
list($search_key, $search_value) = @each($key);
$key = NULL;
foreach ($this->_tpldata[$blockname] as $i => $val_ary)
foreach ($block as $i => $val_ary)
{
if ($val_ary[$search_key] === $search_value)
{
@@ -423,13 +456,13 @@ class template
if ($mode == 'insert')
{
// Re-position template blocks
for ($i = sizeof($this->_tpldata[$blockname]); $i > $key; $i--)
for ($i = sizeof($block); $i > $key; $i--)
{
$this->_tpldata[$blockname][$i] = $this->_tpldata[$blockname][$i-1];
$block[$i] = $block[$i-1];
}
// Insert vararray at given position
$this->_tpldata[$blockname][$key] = $vararray;
$block[$key] = $vararray;
return true;
}
@@ -437,12 +470,13 @@ class template
// Which block to change?
if ($mode == 'change')
{
if ($key == sizeof($this->_tpldata[$blockname]))
if ($key == sizeof($block))
{
$key--;
}
$this->_tpldata[$blockname][$key] = array_merge($this->_tpldata[$blockname][$key], $vararray);
$block[$key] = array_merge($block[$key], $vararray);
return true;
}