tags:

views:

43

answers:

3

I'm trying to find a tool that I can use to generate markup programatically (non-irl word). Here is an example of what I'm trying to accomplish in pseudo-code...

$htmlDoc = new HTML_Document();

$htmlDoc->setTitle('Cool Title');

$htmlDoc->addJs('some_js_file.js');
$htmlDoc->addStylesheet('some_css_file.css');

$divElement = new HTML_Element_Div();
$htmlDoc->append('body', $divElement);

$output = $htmlDoc->generate();

What is the best tool for this?

+2  A: 

Writing your own simple framework is very simple and is a great learning experience. You can implement this in merely an hour or two, in fact I just did for a recent project only a couple weeks ago.

What I recommend is:

  1. Create a simple .HTML template with placeholder tags, I use the syntax %HTML_TITLE% and your setTitle method simple does a STR-REPLACE.
  2. The same holds true for JS, CSS
  3. Since you may not add certain pieces everytime it's important than it your generate() function you remove any remaining %% tags with a %\w+% regex.
TravisO
I'm building a framework based off kohana's cascading file system. I want to delegate many of the framework libraries (layout, db persistence, input, etc...) to well-known, mature, libraries. Thanks for the good suggestion though!
John Himmelman
+1 for learning through doing.
Frank Shearar
+1  A: 

i'm not aware of such a library, but it would be quite easy to write one, for example, based on XMLWriter.

That said, it's worth noting that php was especially designed to avoid things like this. I'd suggest a templating engine or even 'pure' php for html output.

stereofrog
+2  A: 

There's one bundled as standard in PHP: http://php.net/manual/en/book.dom.php

C.

symcbean