Anyone Interested in a PHP Fluent Interface for Generating HTML?

For the past few days I’ve been toying with a set of classes in PHP that is basically a fluent interface for generating HTML. A basic usage example is:

echo p(b('This'), ' is a ', i('test'))->addClass('foo')->html();

or alternatively:

echo new Paragraph(
       new Bold('This'),
       ' is a ',
       new Italic('test'))->addClass('foo')->html();

or

echo new Paragraph()->append(new Bold('This'))
                    ->append(' is a ')
                    ->append(new Italic('test')
                    ->addClass('foo')
                    ->html();

which outputs

<p class="foo"><b>This</b> is a <i>test</i></p>

My goals in doing this were to:

  • Create a terse fluent interface for gnerating HTML;
  • Create valid HTML. This code currently implements the HTML 4.01 Transitional standard so it'll generate an error if you try and append a <p> element to an <object> element, for example;
  • Correctly handle HTML escaping implicitly; and
  • Provide a level of jQuery like manipulation functionality.

I did this largely to see how it would turn out. Basically for my own curiosity. I don't even know if I'll be using this with what I'm working on but just in case I have to ask: is this something others might be interested in?

I haven't seen anything like it but that doesn't mean it hasn't already been done. If there is interest in it, even if its just idle curiosity, I'll clean it up and open source it.

6 comments:

Anonymous said...

The biggest problem I see here, is it's actually less terse than just echoing the HTML. In my experience, classes for building 'constructs' like tables, or ordered lists, etc. make more sense, since you can then pass arrays that the class can walk, using the array structure to build a complex, nested, HTML entities.

Also, using a class to populate 'templates' can be very helpful. Like when you have to build MANY pages, all with the same basic organization, maybe a content pane or something that varies.

Anonymous said...

I think this could be very interesting. Your example has the disadvantage that the generating code is more verbose and longer than the very simple code it generates. I would like to see something like this used to abstract away some of the common javascript patterns, maybe provide a high level php interface into jquery functions, so that a php developer can make good dynamic javascript pages without needing to know any javascript at all.

index.php said...

Interesting programming exercise, but as noted in the first post it wont be as terse as echoing the html.

Konrad Rudolph said...

I like it. It might not be as terse as HTML when used for literals – but HTML in PHP rarely is. Rather, it will be sprinkled with variables and logic. There are several solutions to this, from heredoc to Smarty to plain PHP. The fluent interface above is certainly a very good alternative to all these. And even if it’s more verbose, don’t forget that this isn’t a literal, rather, it’s code with all its benefits (and, admittedly, drawbacks). The only thing I kind of disagree with is the `html` builder function. Why not overwrite the `__toString` magic method additionally/instead? That said, I’d *absolutely* consider using such an interface if I ever have to work with PHP again.

Darryl said...

I would think this idea would be a great addition to the world. At the very least if we do not come up with a direct use for it, someone else may. If these elements were tied together with a history or index mechanism so that it allows items valid in the current container for the markup selected guaranteeing the validity of the markup every time, that seems to me worth the work of engineering the system. Another possible use is to dynamically add java script includes to the head of a page (or wherever) based on the functionality of the page and their dependencies. you used function xaa() out of this file, and it needs this file, add those two in there. No need to hand optimize this, put it together based on what is needed by the body.

Anonymous said...

Check out http://code.google.com/p/php-pear-html-toolkit/ It does almost exactly that with a (very consise) fluent interface. My personal preference would be to use it sparingly alongside something Like Zend_View. Comments welcome (still early stages). -- Damien

Post a Comment