I'm writing a small web app as a side project. It's done in PHP.
Boy, how I loathe PHP.
Well, actually, I don't hate PHP per se. I can't stand HTML intermixed with code. I can barely look at one of those templates without feeling nauseated. I know, when you have an army of "web designers" at your disposal, and you are the only developer, it makes sense to use some templating system. I heard about Smarty a lot. But in my case it has been always the source of yet more work.
Anyways, this time I'm going solo, so I don't want to touch an HTML template with the proverbial pole. So what I'm currently doing looks like this:
<?php
$page = new html_page('My wonderful page');
$page->add_contents(new html_tag('p', 'It works', array('id' => 'helloworld', 'class' => 'somecssclass')));
echo $page->render();
?>
Everything belongs to a nice hierarchy of objects, which is good and dandy. Of course I have a lot of smaller classes, and I'm thinking of using dynamic classes (for example, 'html_a' will automagically create an html_tag object of type 'a'.)
Now, my question: it seems that nobody else is doing this. Why? I'm too far out there, and my feeble, oxygen deprived mind is missing something from the big picture?
(I clearly remember an open source library that did exactly this, but can't find it anymore. So unless I'm actually imagining things, I'm not the only one who thought of this approach to render HTML.)
Do you have any thoughts on this? (please refrain to recommend another language; my favorite language is Perl, so I can outfan you easily :-). I'm stuck with PHP.)
Edit
Uh-uh, seems that I struck a nerve. Some clarifications:
- I'm the only developer in this project.
- How I'm mixing code with HTML? An "html_tag" object from my library is pretty similar to, say, a node in the DOM. The "render" method is the one that creates html (beautifully indented, I must add) but I don't write any opening or closing tag anywhere.
- I create small objects for several tasks. These objects have methods to build tag objects; these resulting objects are then inserted into, say, tables or pages.
- Did I mention that I'm stuck with PHP?
- My library have some primitive access methods to find objects. So the iterator example posted in 26288 can be implemented with relative ease.
- I'm not worried about performance (yet.) I've always thought that's a really nice problem to have, but I'm not there yet.