template engine - nested blocks
Posted: Sat Nov 17, 2007 6:13 pm
I'm developing a template engine. To replace template blocks such as..
I can handle that, by doing the following, using just an example of 3 names
And then in the template object, I can handle that by doing
Then, in my display() method, I can grab just the one block of HTML from the .tpl file, and turn it into however many items are in that array.. here's that snippet of code
So, from that piece of code, my original tpl turns into
All of the above works fine, I just wanted to explain what I am doing.
My problem comes when I have nested "blocks"
I just can't figure out the logic to it. I'm pretty sure it has to be a more than 2-demensional multi-demensional array.
Here's a sample of a nested .tpl block
Now, the categories come out just fine, but they have forum row blocks nested in between. But I can't figure out how to display the forum row blocks for the appropriate category. =[
Right now, all forum rows are showing in each category.
I hope I've explained my problem well. Any help?
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?