Code: Select all
<!-- Start Name //-->
<p>My name is {name} and I am {years} years old.</p>
<!-- End Name //-->Code: Select all
$tpl->assignBlock(
'Name',
array(
'name' => 'Scott',
'years' => 22
)
);
$tpl->assignBlock(
'Name',
array(
'name' => 'Jim',
'years' => 75
)
);
$tpl->assignBlock(
'Name',
array(
'name' => 'Ann',
'years' => 40
)
);Code: Select all
public function assignBlock($blockName, $values)
{
$this->_blocks[$blockName][] = $values;
}Code: Select all
//replace blocks
foreach ($this->_blocks AS $block => $blockValues)
{
//get block
$block = preg_match("/<!-- Start $block \/\/-->(.+?)<!-- End $block \/\/-->/ism", $tpl, $matches);
$block = $matches[1];
//new block for each iteration
$newBlocks = array();
foreach ($blockValues AS $blockValue)
{
$newBlock = $block;
foreach ($blockValue AS $key => $value)
{
$newBlock = str_replace('{' . $key . '}', $value, $newBlock);
}
//save block
$newBlocks[] = $newBlock;
}
//implode blocks back to string
$newBlock = implode("\n", $newBlocks);
//replace original block with replaced looped block
$tpl = str_replace($block, $newBlock, $tpl);
}Code: Select all
<p>My name is Scott and I am 22 years old.</p>
<p>My name is Jim and I am 75 years old.</p>
<p>My name is Ann and I am 40 years old.</p>All of the above works fine, I just wanted to explain what I am doing.
My problem comes when I have nested "blocks"
Here's a sample of a nested .tpl block
Code: Select all
<table width="100%" cellspacing="2" cellpadding="2" border="0" style="border: solid 1px #000;">
<!-- Start Category //-->
<tr>
<td colspan="4" class="cat_name">{cat_name}</td>
</tr>
<tr>
<td width="60%" class="titletext">{L_FORUM}</td>
<td width="10%" class="titletext">{L_TOPICS}</td>
<td width="10%" class="titletext">{L_POSTS}</td>
<td width="20%" class="titletext">{L_LAST_POST}</td>
</tr>
<!-- Start Forum Row //-->
<tr>
<td colspan="4" class="forum_row">{forum_name}</td>
</tr>
<!-- End Forum Row //-->
<tr>
<td colspan="4"> </td>
</tr>
<!-- End Category //-->
</table>Right now, all forum rows are showing in each category.
I hope I've explained my problem well. Any help?