mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-29 06:38:52 +00:00
[ticket/14944] Add possibility to search for template loop indexes by key
Adds a new function to the template interface, and implements it in the context class. The function returns the ordinal index for a specified key, with the same structure that the key for alter_block_array. Reuses same code. Remove unneeded references, do nothing for int keys. Check out of bounds or wrong blockname errors. Added tests. PHPBB3-14944
This commit is contained in:
parent
c656bd60ef
commit
20c03cccdd
2 changed files with 51 additions and 2 deletions
|
@ -305,16 +305,28 @@ class context
|
||||||
$name = $blocks[$i];
|
$name = $blocks[$i];
|
||||||
$index = sizeof($block[$name]) - 1;
|
$index = sizeof($block[$name]) - 1;
|
||||||
}
|
}
|
||||||
|
if (!isset($block[$name]))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
$block = $block[$name];
|
$block = $block[$name];
|
||||||
|
if (!isset($block[$index]))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
$block = $block[$index];
|
$block = $block[$index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isset($block[$blocks[$i]]))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
$block = $block[$blocks[$i]]; // Traverse the last block
|
$block = $block[$blocks[$i]]; // Traverse the last block
|
||||||
|
|
||||||
// Change key to zero (change first position) if false and to last position if true
|
// Change key to zero (change first position) if false and to last position if true
|
||||||
if ($key === false || $key === true)
|
if ($key === false || $key === true)
|
||||||
{
|
{
|
||||||
return ($key === false) ? 0 : sizeof($block);
|
return ($key === false) ? 0 : sizeof($block) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get correct position if array given
|
// Get correct position if array given
|
||||||
|
@ -331,7 +343,7 @@ class context
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return is_int($key) ? $key : false;
|
return (is_int($key) && ((0 <= $key) && ($key < sizeof($block)))) ? $key : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -603,6 +603,43 @@ EOT
|
||||||
$this->assertEquals($expect, str_replace(array("\n", "\r", "\t"), '', $this->display('test')), 'Ensuring S_NUM_ROWS is correct after modification');
|
$this->assertEquals($expect, str_replace(array("\n", "\r", "\t"), '', $this->display('test')), 'Ensuring S_NUM_ROWS is correct after modification');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_find_key_index()
|
||||||
|
{
|
||||||
|
$this->template->set_filenames(array('test' => 'loop_nested.html'));
|
||||||
|
|
||||||
|
$this->template->assign_var('TEST_MORE', true);
|
||||||
|
|
||||||
|
// @todo Change this
|
||||||
|
$this->template->assign_block_vars('outer', array('VARIABLE' => 'zero'));
|
||||||
|
$this->template->assign_block_vars('outer', array('VARIABLE' => 'one'));
|
||||||
|
$this->template->assign_block_vars('outer.middle', array('VARIABLE' => '1A'));
|
||||||
|
$this->template->assign_block_vars('outer', array('VARIABLE' => 'two'));
|
||||||
|
$this->template->assign_block_vars('outer.middle', array('VARIABLE' => '2A'));
|
||||||
|
$this->template->assign_block_vars('outer.middle', array('VARIABLE' => '2B'));
|
||||||
|
$this->template->assign_block_vars('outer', array('VARIABLE' => 'three'));
|
||||||
|
$this->template->assign_block_vars('outer.middle', array('VARIABLE' => '3A'));
|
||||||
|
$this->template->assign_block_vars('outer.middle', array('VARIABLE' => '3B'));
|
||||||
|
$this->template->assign_block_vars('outer.middle', array('VARIABLE' => '3C'));
|
||||||
|
|
||||||
|
$expect = 'outer - 0 - zero[outer|4]outer - 1 - one[outer|4]middle - 0 - 1A[middle|1]outer - 2 - two[outer|4]middle - 0 - 2A[middle|2]middle - 1 - 2B[middle|2]outer - 3 - three[outer|4]middle - 0 - 3A[middle|3]middle - 1 - 3B[middle|3]middle - 2 - 3C[middle|3]';
|
||||||
|
$this->assertEquals($expect, str_replace(array("\n", "\r", "\t"), '', $this->display('test')), 'Ensuring template is built correctly before modification');
|
||||||
|
|
||||||
|
$this->template->find_key_index('outer', false);
|
||||||
|
|
||||||
|
$this->assertEquals(0, $this->template->find_key_index('outer', false), 'Find index at the beginning of outer loop');
|
||||||
|
$this->assertEquals(1, $this->template->find_key_index('outer', 1), 'Find index by index in outer loop');
|
||||||
|
$this->assertEquals(2, $this->template->find_key_index('outer', array('VARIABLE' => 'two')), 'Find index by key in outer loop');
|
||||||
|
$this->assertEquals(3, $this->template->find_key_index('outer', true), 'Find index at the end of outer loop');
|
||||||
|
$this->assertEquals(false, $this->template->find_key_index('outer', 7), 'Find index out of bounds of outer loop');
|
||||||
|
|
||||||
|
$this->assertEquals(false, $this->template->find_key_index('outer[0].middle', false), 'Find index at the beginning of middle loop, no middle block');
|
||||||
|
$this->assertEquals(false, $this->template->find_key_index('outer[1].middle', 1), 'Find index by index in inner loop, out of bounds');
|
||||||
|
$this->assertEquals(1, $this->template->find_key_index('outer[2].middle', array('VARIABLE' => '2B')), 'Find index by key in middle loop');
|
||||||
|
$this->assertEquals(2, $this->template->find_key_index('outer.middle', true), 'Find index at the end of middle loop');
|
||||||
|
|
||||||
|
$this->assertEquals(false, $this->template->find_key_index('outer.wrong'), 'Wrong middle block name');
|
||||||
|
$this->assertEquals(false, $this->template->find_key_index('wrong.middle'), 'Wrong outer block name');
|
||||||
|
}
|
||||||
public function assign_block_vars_array_data()
|
public function assign_block_vars_array_data()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
|
|
Loading…
Add table
Reference in a new issue