XPML is an eXtensible PHP Markup Library. It provides methods for easing the production of syntatically correct markup while keeping the PHP code it is nested in visually coherent. XPML seeks to simplify the PHP/HTML coding experience.

There are several examples of for using XPML in the CVS tree under xpml/examples. In fact, this page was created using XPML and its code can be found in xpml/expamles/savannah.nongnu.org/projects/xpml.

XPML started as a way to bring some of the ease of markup coding in Kevin Rosenberg's LML from the land of Lisp, which is unfortunately not usually supported on most web hosts, to PHP, which usually is. It was never a Lisp to PHP translation kind of thing, but more of a functional programming approach to html generation in PHP. Two features of LML which I found very useful were 1) the dynamic creation of markup generating functions and 2) the functional style of creating markup.

The main xpml class accomplishes the first goal: dynamic creation of the markup generating functions. It provides several options for their creation: global, class, and prefix. The global option creates the functions like 'html()' and 'p()'. This option has the benefit of being the easiest to type, but the downside of namespace conflicts with other functions (such as 'var'). The class option has the opposite effect. The namespace is neatly encapsulated in a class of your choosing, but it requires more typing to access those functions. The prefix option can be combined with both, but will most likely see use in conjunction with the global option. It prepends all functions with a prefix of your choosing. It constitutes something of a compromise between the global and class options.

Once the xpml class produces the functions their use then accomplishes goal number two. This can best be seen through an example. Assume that xpml has been used to create a basic set of html functions with the prefix 'x'. The following code would then create a table row:

xtr(xtd('data 1'), xtd('data 2'))
It bears noting here that all the functions created by xpml are designed to take a variable number of arguments. The code above produces a tree of objects. When those objects are rendered to text it will be nicely indented to produce this:
  data 1
  data 2

One of the benefits to the functional sytle when generating your markup is that you don't have to close tags manually. Further, the nuance of closing single tags like <br /> is taken care of as well. Add in the use of an editor with paren matching and you can tell exactly which tag you're closing when you end your functions (not to mention, again, that you don't have to write out </tagname>).

Besides the pragmatic benefits, there is the aestheticly pleasing 'tidy effect' when using XPML. The PHP code looks more coherent, at least to me ;o} Depending on your editor the nested function calls are automatically indented in a way that mimics what the end html will actually look like. There aren't as many HTML->PHP, and vice-versa, transitions.

Don't get me wrong! There are some drawbacks... PHP wasn't really designed around a pure functional style. This results in some weirdness when you want to put things like 'if' and 'while' inside a function call. Actually, you can't directly put an 'if' in a function call (at least it didn't work last time I tried it). So you have to wrap the if in a function. PHP 5 does support a kind of lambda functions and they would make good use in these situations. Unfortunatly 'create_function' (the PHP 5 anonymous function creator) does not have the same syntax as typical function declarations and consequently doesn't look as clean as it otherwise might.

Anywho, I hope you enjoy using XPML!