My head's spinning so far.
I looked through phpbb's template code, and I like their idea of using "." to separate block names.. instead of using a multi-dimensional array for the blocks. This way, all of the block names would be on the first level of the main array.
So, say I had this code:
Code: Select all
//assign categories
$arr['category'][] = array('name' => 'cat1');
$arr['category'][] = array('name' => 'cat2');
$arr['category'][] = array('name' => 'cat3');
//assign category forums
$arr['category.forum_row'][] = array('foo' => 'foo', 'bar' => 'bar');
$arr['category.forum_row'][] = array('foo' => 'foo2', 'bar' => 'bar2');
$arr['category.forum_row'][] = array('foo' => 'foo3', 'bar' => 'bar3');Code: Select all
//Misc. class data
//private $_blocks = array(); //holds block data
//private $_blockCodes = array(); //holds each unique block's code in the format of blockname => blockcode
//$tplCode = file_get_contents($templateFile);
foreach ($this->_blocks AS $blockName => $contents)
{
if (strpos('.', $blockName)
{
//nested block
$blockNames = explode('.', $blockName);
$blockCount = count($blockNames);
//iterate through each block, building and evaluating the nesting, then replace variables
//in that code with the values for each variable
//maybe this is wrong. maybe I need to nest in reverse?
//something... my head is spinning ;(
for ($i=0; $i<$blockCount; $i++)
{
$thisNest = array_slice($blockNames, 0, $i, true);
//build nest
$nestStr = '$this->_blocks';
foreach ($thisNest AS $nest)
{
$nestStr .= '[\'' . $nest . '\']';
}
//evaluate the string
eval($nestStr);
//get block name
$thisBlock = $blockNames[$i];
//get block template code
$blockCode = $this->_blockCodes[$thisBlock];
//parse block of code into string
foreach ($nestStr AS $var => $value)
{
$code = str_replace('{' . $var . '}', $value, $blockCode);
}
//place the evaluated block of code back into the template
$tplCode = str_replace($this->_blockCodes[$thisBlock], $code, $tplCode);
}
} else
{
//rootlevel block
//replace contents with vars/values
foreach ($contents AS $var => $value)
{
$code = str_replace('{' . $var . '}', $value, $code);
}
//place the evaluated block of code back into the template
$tplCode = str_replace($this->_blockCodes[$blockName], $code, $tplCode);
}
}I've spent a couple hours straightening out my thoughts in notepad.. so.. comments, suggestions?
Am I overcomplicating things?