tags:

views:

52

answers:

3

Hi

Iam looking for a php parser that can do this.

{tag} Replace the tag with text comming 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

Do anyone of you know of an parser that can do this?

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

Smarty seems to be a bit to big, and i dont know if you can disable all the extra functionality it has. I only need the functionality that i have listet over. In smarty your able to write php code and i dont like that. {php} {/php} So if iam going to use that i need to be able to turn it of.

(Iam going to use it with codeigniter.)

A: 

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.

Kamil Szot
A: 

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.

serg
A: 

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.

icio