PDA

View Full Version : Another Template Parser in PHP


Cameron
April 10th, 2006, 23:44
Note: if the subject of PHP, Template Parsers, Objects, different Template Syntax, etc, make you yawn or go "huh?", this topic may not be something you're interested in reading, you've been warned (and I can be boring!).

<rant>
There are literally forums dedicated to debating/arguing on which template parser is the correct way, or if, they should even exist. Or how X is better then Q. Of course, everyone holds an opinion on such topics, because they can, so with that being said, I really do not care for your opinion, and as such, this is not the place to voice it, if you want to voice it, start a new topic, rant about it in your blog [stupid word], but don't post it here, I don't care, and this isn't the place.
</rant>

So, I apparently have this hobby, that involves torturing myself; which usually begins with me becoming un-happy about something i've written, or someone else has, and at one time was found to be useful. However, all good things must come to an end, and the inner mad scientist comes out and destroys those happy times, and starts planning, and tinkering, and well causing mayhem among the locals. My newest thing to re-engineer (you know it, I know it, they know it, its exactly what i'm trying to do), is the Template Syntax I "use", (which is akin to phpLib, and phpBB's implementation), which in itself is good and all, but i've never been to happy with it.
Example of "offending" syntax:<ul>
<!-- BEGIN this_loop -->
<li><a href="{this_loop.URI" title="{this_loop.TITLE}">{this_loop.TITLE}</a>
<ul>
<!-- BEGIN inner_loop -->
<li><a href="{this_loop.inner_loop.URI" title="{this_loop.inner_loop.TITLE}">{this_loop.inner_loop.TITLE}</a></li>
<!-- END inner_loop -->
</ul>
</li>
<!-- END this_loop -->
</ul>
Now, there is nothing wrong with the above, example it seems pretty clear to me, and probably you as well. However, my qualms lie in the fact, if the array assigned to inner_loop is empty, you'll end up with an empty un-ordered list, which just looks kind of silly to me. Thirdly, or secondly for those playing the home game; the use of HTML comments to define Template Syntax, yes, it is readable, but I just don't really like it so much anymore.

A few months ago, I came across yet another template parser called PHPTAL (http://phptal.motion-twin.com), which is the PHP implementation of Zope Page Templates, which is used in Zope (http://www.zope.org/WhatIsZope) (click the link to find out what Zope is). It really is a nice idea, that builds upon more or less existing technology found in XML (and subsequently XML). Those whacky people at Zope came up with the ideal of Template Attribute Language Expressions, which essentially removes Template syntax from stand-alone comments or tags such as <!-- BEGIN loop --> (ala phpLib/phpBB), or <MTEntries> (ala MovableType), and moves them into the XML/XHTML source.

Example of "TAL" syntax:
<h1 tal:content="pageTitle">Default Page Title</h1>
<ul tal:repeat="myVar out/inner">
<li><a href="#exampleuri" tal:attributes="href myVar/uri" tal:content="myVar/inputvalue">Default Value</a></li>
</ul>
Which seems faily simple to me, and allows you to more or less see what the template will look like, even without being parsed. Now PHPTAL's features and what it can do is quite extensive [PHPTAL Manual (http://phptal.motion-twin.com/manual/en/split/)], however a lot of it seems like over-kill to me. Imagine shoving a Chevy 350 w/ 400HP into a 1968 Volkswagon Beetle, doable, but overkill none the less (and yes, people really do put those into Volkswagon Beetles, its kind of scary), and you can see what I'm more or less thinking. I can imagine where Macros could come in handy, and built-in internationalization is nice, but not everyone has PHP built against gexttext.

Now, above I did say it uses technology found in XML, which it does, it uses a custom namespace, and it also uses something that sorta looks like Xpaths (a hint: they're not Xpaths, its just a different way to traverse an array thats been assigned).

Why not just use PHPTAL?:
I enjoy torturing myself, plus like I said above, its more then I want, and the portion of concepts I like about it, are only a small subset of it.

Can I help you?:
Yes, but no (and no, i'm schizo). Yes, you can tell me this is a bad idea, this is a good idea, provide suggestions (providing they arn't 1.) asanine 2.) absurd). No, you cannot give me code to use right now, currently, theres not even nothing written, so theres nothing you could contribute.

However, when there is something worth seeing, I may accept 3rd party patches from others, providing they meet the guidelines I set forth when that time comes. Which if I do, and your patch(es) do not meet the guidelines, they will be promptly deleted, and you will more then likely be heckled (only because I can).

PHP Requirements:
PHP5, only because I enjoy the look on people's faces when they see something they like, and its just out of their reach. Or, possibly because PHP5 made improvements to things such as XML, and Object handling, are leaps and bounds better then what can be found in PHP4 (granted, its not anything compared to C#, Java, Object-C, etc).
So unless you're host supports both PHP4 and PHP5 with one as a SAPI module, the other as a FastCGI binary, or both as FastCGI binaries, you're pretty much out of luck.

License:
More then likely, this and the framework (oh boy, just what we need, another framework!) will be released under a BSD-like license, however, depending on if I "creatively" re-use any code from PHPTAL, it may be released under the LGPL (Gnu Lesser General Public License) as PHPTAL is.

Dan
April 11th, 2006, 00:30
Why not just have PHP parse the XML with XSL? It doesn't look like the code above has much of an advantage over it, and XSL is already well-documented.