Hi,
I'm building a pseudo-variable parser in PHP to allow clean and simple syntax in views, and have implemented an engine for if-statements. Now I want to be able to use nested if-statements and the simplest, most elegant solution I thought of was to use identation as the marker for a block.
So this is basicly the layout I'm looking for in the view:
{if x is empty}
{if y is array}
Hello World
{endif}
{endif}
The script would find the first if-statement and match it with the endif on the same depth. If it evaluates to true the inside block will be parsed as well.
Now, I'm having trouble setting up the regular expression to use depth in the following code:
preg_match_all('|([\t ]?){if (.+?) is (.+?)}(.+?){endif}|s', $template, $match);
Basically I want the first match in ([\t ]?)
to be placed before {endif}
using some kind of variable, and make sure that the statement won't be complete if there is no matching {endif}
on the same depth as the {if ...}
.
Can you help me complete it?