Reputation: 5212
I'm writing up Yet Another Template Engine in PHP. Should Mustache.php really be 1.6mb?? I want something lighter and completely workflow Agnostic. What happened to the good ole days of including a vendor file and using it?
Anyhow, the syntax is simple and such I have a very simple working preg_replace working for simple variables i.e. [[variable]]
However I'm running into an issue whereas I'd like to to catch encapsulated conditional content. I.e
[[#if this == 'that']]
To be seen or not to be that is the question!
[[/if]]
Below is my current regex and its very close, however I can't seem to work out the proper rules for the closing [[/if]]
tag;
It captures past the closing tag :(
preg_match('/\\[\\[#if(.+)\\]\\][^\\[\\]\\/](.+)\\[\\[\\/if\\]\\]/s', $template, $ifmatches);
Any help would be greatly appreciated!
Upvotes: 1
Views: 133
Reputation: 9591
This is my modification to your regex:
\[\[#(\w+)\s(.*?)\]\](.*?)\[\[\/\1\]\]
Changes that I made:
\w
between 1 and unlimited times. I placed this into a capture group with the backreference number of 1.(.*?)
with backreference number 2.?
quantifier and placed into backreference 3.The thing that really makes this work better is the ?
quantifier. It turns the expression "lazy" which is a good thing, because it doesn't "over-reach".
Here's a demo to show how it works:
Upvotes: 1