Hi,
I’m trying to write a simple template parser for an application I’m building, but I am stuck on something that seems like it should be obvious, but I cant work it out for the life of me.
Say I have a template that looks like this:
Some text here
{loop:products}
Text
{/products}
Some more text
I can easily write a function with preg_replace_callback that will find all the loops and repeat the contents the number of time, or insert data etc., what goes in the loop is irrelevant at the moment. The regex looks like this:
/{loop:(.*?)}(.*?){\/loop}/is
Where I’m stuck is working out how to deal with something like this:
Some text here
{loop:products}
Text
{loop:prices}
Text
{/ loop}
{/ loop}
Some more text
Because the regex will return:
{loop:products}
Text
{loop:prices}
Text
{/ loop}
I get the feeling this can’t be done with preg_replace_callback, but can someone point me in the right direction on how to deal with this? Being able to nest tags seems a fundamental part of any template system, mark-up language etc. I just don’t have a clue how to deal with it.
Thanks in advance, any help will be MUCH appreciated.
Jack