开发者

Looking for a PHP template Parser with nesting

开发者 https://www.devze.com 2022-12-29 07:30 出处:网络
I am looking for a PHP parser that can do this. {tag} Replace the tag with text coming from a function

I am looking for a PHP parser that can do this.

{tag} Replace the tag with text coming from a function

{tag(params)} It must support params

{tag({tag(params)},{tag(params)})} It must support nesting

{tag()?

else

} It must support Tests

{$tag=value} It must support varriables

Does anyone know of a parser that can do this?

Or maybe you know how I can create one. I have tried to do this with preg, but it seems impossible to create nesting.

Smarty seems to be a bit to big, and I don't know if you can disable all the extra functionality it has. I only need the functionality that I have listed above.

In smarty you're able to write PHP code and I don't like that. {php} {/php} So if I'am going to use that I need开发者_Go百科 to be able to turn it of.

I'm going to use it with CodeIgniter.


To incorporate nesting in your regular expressions you have to use recursive patterns (?R). This matches whole pattern.

To find outermost {}-s in text (possibly with some other {} inside of them) you should use expression like this:

/{([^{}]*(?R))*[^{}]+}/

You can read about (?R) here: http://pl2.php.net/manual/en/regexp.reference.recursive.php

There is a construct {((?>[^{}]+)|(?R))*} suggested there. Maybe more efficient than the one I built.


I would just go with Smarty3. Why reinvent the wheel when someone already did years of development, testing and optimization for you?

In Smarty3 {php} tag is disabled by default. It also supports security policies where you can disable php function calls and such if you are letting users modify your templates.


CodeIgniter did this very well before introducing it's built-in templating. CodeIgniter view templates used to be written simply in PHP and thus you were free to use any PHP features you wish: call structures, function calls and more.

I recommend you simply use the View instead of the Template Parser.

0

精彩评论

暂无评论...
验证码 换一张
取 消