Hi everyone
I am currently working on a templating system, but i'm stuck in a function that is supposed to handle loops. The function is build around a three-dimensional array(: $block[$blockname][$block-iteration][$block_replace_keys] = $block_replacement_values). At first sight it seems to be working fine, but when it loops it is always the same values in the fields and sometimes it returns the double amount of loops as it should.
the function that handles the loops looks like this:
protected function HandleBlocks($data)
{
// we set some basic vars
$block_delimiters = array('start' => "<!-- START BLOCK [BLOCKNAME] -->",
'end' => "<!-- END BLOCK [BLOCKNAME] -->");
$handled_blocks = NULL;
while(list($blockname,) = each($this->block))
{
$block_del = str_replace('[BLOCKNAME]', $blockname, $block_delimiters);
$block_del_desc = array('block_start_pos' => strpos($data, $block_del['start']) + strlen($block_del['start']),
'block_end_pos' => strpos($data, $block_del['end']));
$block_length = $block_del_desc['block_end_pos'] - $block_del_desc['block_start_pos'];
$str = substr($data, $block_del_desc['block_start_pos'], $block_length);
$block_iteration = count($this->block[$blockname]);
if(preg_match_all("#<!-- START BLOCK (.*?) -->#", $str, $m))
{
if(is_array($this->block[$m[1][0]]) && ( isset($this->block[$m[1][0]]) || !empty($this->block[$m[1][0]] )))
{
$edit_str = $this->HandleBlocks($str);
}
else
{
$edit_str = $str;
}
}
else
{
$edit_str = $str;
}
if(isset($this->block[$blockname][0]) && $this->block[$blockname][0] == TRUE)
{
for($i = 1; $i < $block_iteration; $i++)
{
foreach($this->block[$blockname][$i] as $blockvar_key => $blockvar_value)
{
if(preg_match("(\{(" . $blockvar_key . ")\})", $edit_str, $match))
{
$edit_str = str_replace($match[0], $blockvar_value, $edit_str);
}
}
}
$handled_blocks .= rtrim($edit_str);
$data = str_replace($block_del['start'] . $str . $block_del['end'], $handled_blocks, $data);
}
}
return $data;
}
In case this function is not the problem, i have noticed that the only time it tends to loops the double amount of times is when i use a function that registers arrays and not single values/keys.
So here's that function:
public function AssignBlockVars($blockname, $vararray)
{
$iteration = count($this->block[$blockname]);
if(isset($this->block[$blockname]) && $this->block[$blockname][0] == TRUE)
{
if($iteration > 0)
{
@reset($vararray);
while(list($k, $v) = each($vararray))
{
$this->block[$blockname][$iteration][$k] = $v;
}
return TRUE;
}
}
return FALSE;
}
Thanks