I am in a middle of writing a project which has a template engine and some new defined tags like LOOP
or IF
and etc ...
Assume this is a block of template that PHP should process and convert to a PHP script:
<LOOP products>
{{name}}
{{id}}
<LOOP comments>
{{name}}
</LOOP>
{{quantity}}
</LOOP>
I want to convert all {{variables}}
to print them out based on the properties of the loop variable, But I want to exclude inner LOOP
tags for each loop.
Because the first LOOP
's {{name}}
tag should be $product->name
and second LOOP
's name should be $comment->name
This regex will convert all {{variables}}
to first LOOP
variable which is product
.
$pattern = '/\s*\{\{(\w+)+\}\}\s*/';
Above output is
<LOOP products>
{{name}} // $product->name
{{id}} // $product->id
<LOOP comments>
{{name}} // $product->name ! <-- this {{variable}} should
// be exculde of first loop converting.
</LOOP>
{{quantity}} // $product->quantity
</LOOP>
UPDATE
I also tried this:
(?!<LOOP[^>]*?>)\{\{(\w+)+\}\}(?![^<]*?</LOOP>)
// this works for 2 level of nested LOOPs.
// when I add another LOOP as third level ...
// ... contents of level2 are changing too, which is not corrent.
// ONLY first level should change.