OK so I'm trying to build a template engine that uses a small amount of simple logic. The issue I encounter is with somekind of if-statement, which I have like this:
{IF [SOMEVAR]}
<p>Hi, how are you?</p>
{END IF}
Fairly easy.
The issue is that these statements can be nested in each other, ideally without having a limited depth. To make it more challenging, the statements can also follow each other up.
An example of a nasty piece that doesn't come out right:
{IF [AAA]}
<p>single AAA</p>
{END IF}
{IF [BBB]}
<p>single BBB</p>
{END IF}
{IF [AAA]}
<p>AAA</p>
{IF [BBB]}
<p>Nested BBB</p>
{END IF}
{END IF}
I got this piece of code working so that it matches the first {END IF} after hitting an {IF. This breaks the nested IF statements.
I also got this working so that it matches the last {END IF}, this breaks even more.
The regex strings I used are these:
'/(\{IF \[)(.+)(\]\})(.+)(\{END IF\})/iUse',
'/(\{IF \[.+?\]\})(.+\{IF \[.+\]\}.*\{END IF\}.*)(\{END IF\})/ise',
I'm not much of a regex pro, so if it can be improved anywhere, that would be great.
Any help is truly appreciated.