A while back I did some work in scheme lisp and was much impressed by the readability of the code which generates the HTML – basically a nice nested tree of self closing tags, where you can build up levels of abstraction, yet stay close to the exact HTML your rendering and have control over it.

Then I had to go back to PHP for pragmatic reasons, and I just didn’t gel with any of the frameworks or tag libraries around. I experimented with various template engines but it seemed annoying to break syntax and use yet another language. I did find an implementation of lisp in PHP, which I though was nifty, but the people who hired me as a consultant were eagerly awaiting their code bundle in PHP.

PHP just wont die. It may not be an elegant language but its the workhorse of the internet.

I had to find a way to close tags automatically and keep everything readable. There’s a lot of bad PHP code around, where the script basically progresses from the top of the web page downwards, mixing php code snippets with HTML, javascript and SQL statements – not a recipe for clarity.

The nested xilla_tag functions build up a tree built with PHP arrays.  This tree is then walked and HTML tags generated when you call render_tags().  As the tree is written out, the end tags are automatically added, and the resulting output indented.

Example Comparison

To make a valid comparison, I use the same HTML sample given in another tag library, namely HAML – see phphaml.sourceforge.net

Compare ‘Old school’ PHP program code –

non lispy php code

To lispy php code using xilla tags. Read from the bottom sample_page() upwards –

lispy php

lispy code body


I’ve uploaded xilla_tags to google code. You can use it freely under a permissive BSD licence – available on github here.

I hope you find it a more readable way of generating your web page programs in PHP,



About these ads