View Full Version : Need help on template parser class

04-24-2004, 04:25 PM

at the moment I'm working on a very simple template parser class. It is nothing fancy, but it does its job. The way it works is as follows:

1) I've got a template file that I read into a variable.
2) In the template file variables are marked by %VARIABLENAME% and blocks of code are marked by code
3) Using regular expression I collect all the blocks into an array.
4) I pass the variable output (array) with all the appropriate values (build by php) to a function that replaces the values with the script.
5) All blocks and variables that are not used are deleted from the template and everything is printed to the screen.

Okay, so far it works, but now I want to add an extra feature to the script. I want to be able to have sub-blocks within blocks. For example:




Now my question is how I should adjust the regular expression and wat is a smart way to store the different (sub-) blocks? Any thoughts on this?

Code snippet:

function _GetBlocks() {
$reg_exp = "|(\[[^\]\/]+\])([^\[\]]+)(\[[\/][^\]]+\])|s";

preg_match_all($reg_exp, $this->_sTpl, $match);

foreach($match[1] as $key=>$value) {
$blockname = trim($value, '[,]');
$blockcontent = $match[2][$key];
$this->_aBlocks[$blockname] = $blockcontent;
$search = '['.$blockname.']'.$blockcontent.'[/'.$blockname.']';
$replace = '%'.$blockname.'%';
$this->_sTpl = str_replace($search, $replace, $this->_sTpl);

I hope I have been clear enough! If not please let me know and I'll try and clarify it.

Thanks for your help. Michiel