Jordan Arsenault
Jordan Arsenault

Reputation: 7408

PHP Nested Template Parser

I need a PHP template parser that is capable of parsing the following format:

{users}
    {name}
    {age}
    {posts}
        {title}
        {body}
        {comments}
            {comtitle}
            {combody}
        {/comments}
    {/posts}
{/users}

All template parsers I've looked into... are either not recursive enough or come with unnecessary template constructs that look so similar to PHP syntax that it's not worth it. This is the default way CodeIgniter templates work, but they do not dig down deep enough.

Believe it or not, none of the parsers in this blog post appear to be able to do this. Some can achieve it, but with a syntax that approaches PHP's native foreach function.

Do you know if a parser exists? Or have you created one that reads this type of structure? The desired parser should be able to dig down infinitely further provided that an even more nested array was provided to it.

Specifically, I'm looking for a CodeIgniter solution, but any PHP solution will suffice at this point as I've been investigating this for a few days now.

Thanks!

Upvotes: 0

Views: 1299

Answers (3)

Barnabas Kecskes
Barnabas Kecskes

Reputation: 1961

"I don't have tests to back this up but I would assume this is a joke for a computer." @Jordan Arseno

Jordan, I agree. Although if you have quite a few files to parse at the same time, it can all add up pretty badly. I agree with you having your own template parser if you have the drive / special needs for that. Although using a little bit different syntax can save you a lot of processing time during parsing...

Instead of using this version:

{users}
    {name}
    {age}
    {posts}
        {title}
        {body}
        {comments}
            {comtitle}
            {combody}
        {/comments}
    {/posts}
{/users}

You could just as well use something like:

{foreach: users}
    {name}
    {age}
    {foreach: posts}
        {title}
        {body}
        {foreach: comments}
            {comtitle}
            {combody}
        {endforeach: comments}
    {endforeach: posts}
{endforeach: users}

This way when finding the {foreach:<array>} pattern, you will KNOW that there is a matching tag, and you won't have to TRY to find one for all patterns. You can then easily use preg_match() to make your way through.

(Note: you can still do the same with your style, although you will have to parse the code backwards which would make it really inconvenient I guess.)

Upvotes: 0

Jordan Arsenault
Jordan Arsenault

Reputation: 7408

I've decided to completely give up on third-party template parsers and opt for using PHP's native support. The headache associated with compatibility, deep nesting, conditionals, loops, all within my own specification is not worth it.

From this point forward, I will be briefly teaching my designers loops and conditionals for complicated views. For anybody using CodeIgniter, I strongly recommend enabling short-tags and taking advantage of CodeIgniter's Optional Syntax for echoing out data:

Instead of: <?php echo $variable; ?>

One can write: <?=$variable?>

Look how close this approaches a standard template language: {variable}

Start making use of PHP's Alternative Syntax For Control Structures so that your code is not littered with echo statements and long PHP strings of HTML.

IMO this is the templating solution. Though your designers will hate you for introducing:

if, elseif, endif, foreach, and endforeach, an hour discussion with them explaining how they work with examples will give you the most flexibility in your views.

Go nuts!

Upvotes: 0

user149341
user149341

Reputation:

As written, that format looks impossible to parse. There's no way to distinguish between a {tag} which will have a matching {/tag} (like {posts}) and one which won't (like {body}).

Don't try to reinvent the wheel. There are a lot of good template parsers out there, along with PHP itself. Unless you're really good, anything you come up will probably not be as featureful, nor as fast, as what's out there already.

Upvotes: 1

Related Questions