Code: Select all
{variables}
{include: file}
{* comments *}
{block: blockname}
[i]block-content[/i]
{blockname/}
But I've ran into the following problem: how to code nested blocks in PHP? I tried:
Code: Select all
for($blockname = 0; $blockname < $this->_blocks['blockname'][$parent1iteration][$parent2iteration]; $blockname++) ...
My current _parse() of the template class (I excluded all the non-block parsing parts):
Code: Select all
/**
* Parses content, $this->_content is the raw template file
*/
private function _parse() {
/**
* Parse blocks
*/
$this->_content = preg_replace_callback($this->_regex['block'], array('self', '_parseBlock'), $this->_content);
/**
* Put files block structure on top, because we have to remember our blockstructure
*/
$structure = '/*block structure*/ $this->_blocks = array_merge($this->_blocks, array(';
foreach($this->_blocks as $block => $childs) {
$structure .= '\'' . $block . '\'=>\'' . $childs . '\',';
}
$structure .= '));';
$this->_content = $this->_phpOpen . $structure . $this->_phpClose . $this->_content;
}
/**
* Parses blocks
* @param $blockContent
* @param $root
* @return parsed block
*/
public function _parseBlock($blockContent, $root = NULL) {
$block = $blockContent[1];
if(!isset($root)) {
$root = $block;
}
else {
$root .= '.' . $block;
}
$this->_blocks[$block] = $root;
/**
* Find child blocks (continue while match found)
*/
$childContent = $blockContent[2];
if(preg_match($this->_regex['block'], $childContent) == 1) {
$childContent = preg_replace_callback($this->_regex['block'], create_function('$matches','global $tpl; return $tpl->_parseBlock($matches, \'' . $root . '\');'), $childContent);
}
/**
* Now make the real code change, first we get the root needed by for-statements
*/
$blockRoot = explode('.', $this->_blocks[$block], -1);
$blockRoot = (!empty($blockRoot) ? '[$' . implode('][$', $blockRoot) . ']' : '');
$blockRoot = '$this->_blockCount[\'' . $block . '\']' . $blockRoot;
return $this->_phpOpen . 'for($' . $block . ' = 0; $' . $block . ' < ' . $blockRoot . '; $' . $block . '++) {' . $this->_phpClose . $block . $childContent . $this->_phpOpen . '} /*end ' . $block .' loop*/' . $this->_phpClose;
}