html generator

today i wrote a stripped down html generator (in php, of course). there’s got a be a jazzier word than generator. you know something that’s the opposite of a parser. something that models and spits out a tree.

it has the ability to generate a single HTML tag with attributes and optionally some content and an end tag. like this:

<acronym title="mainstream media">msm</acronym>

or this:

<img src="some_image.jpg" />

that’s it. in all its glory. seems totally worthless, huh? but the beauty is that it can represent any tag (element actually) with any attributes (in other words it will create well-formed but not valid html). so it’s pretty flexible.

and the contents (“msm” in the first example above) can be either text and/or other elements which can in turn contain text and/or other elements. so i can use it to model a whole webpage, since every webpage is at the root composed of a single html element:

<html>justin <em>blathering</em> about some techie junk</html>

it’s kind of modeled on my rudimentary understanding of the dom with a much simpler/cleaner interface. it doesn’t have any fancy tree traversing methods yet but i’m getting there.

anyway, here’s the code: HTMLElement.phps.

update: in php 5, the new dom functions should accomplish what i’ve intended with my code.

19 Comments

where’s it going? the article you posted about ajax is quite interesting by the way, i’m glad there’s a term for the idea – i’ve been pondering using something similar in one of my projects…

well, i do a lot of html table building it at work (laying out financial data, reports) and so i need this to build up some fairly complicated reports. but really it was a process of simplification, taking a whole bunch of redundant classes that i had created over the course of the day and realizing they all could be reduced down to a single HTMLElement.

i think the beauty is that you can call the “to_html” method on any element and it will trigger the “to_html” of all of it’s children, and so on. and that children don’t have to be other HTMLElements, they can of course be text (aka CDATA).

ps. here’s that article on google maps: mapping google

ryan

i read that yesterday, good article. there are some table classes already written that i’ve seen, though i can’t remember their names

yeah, well there’s pear which i’ve kind of been looking via the web, but really haven’t messed with any of the code yet.

i think im missing the point because i don’t know the context you are using it in. “html table building”? Why don’t you just use the dom extension?

so i went looking for more stuff – found enough to post about

ha! i figured this would happen, considering that there are CXL (140!) different php libraries… corey, the simple answer is i didn’t realize it existed.

ps. what’s the difference between dom and dom xml?

nevermind, dom xml is essentially deprecated, so stick with dom

and finally, holyshit! the dom module is complicated. this is going to take some digesting.

i tried example 1 on this page and can’t get it to work in php 4.1.2 (at work) or 4.3.10 (at home). apparently the dom module is php 5 only. and the html_dump_mem example of this page doesn’t work with either. hmm.

jackie

geez, get a [chat] room!

hey if you’re still having problems with the dom, there’s support in PHP 4.1.0 for an XSL parser, full article about it here

i have to admit, i have been lax to buff up on my practical knowledge of xsl/xslt. it’s like sometimes i need to just mentally ignore certain topics so i can focus my attention on others.

i know what you mean, it seems like no matter how much you know there’s going to be a huge community of people talking in acronyms you’ve never heard and arguing about things you don’t understand. i have no idea, for instance, what the .NET framework is.

jackie

oh, i totally know what you mean. ;)

i think this might have been a dead end. i was trying to get it so that once i created the html fragment (of a table), i could use a method like getElementsByTagName which would return an array of references to certain objects that I then could loop through and selectively modify.

for example i would want to change the colspan of a cell that’s set to 0 to be the actual number of columns. i got the getElementsByTagName method working and it even looked like the method returned an array of references to the correct objects, but then when I foreach through those objects and run a method on the object, the change i doesn’t stick. agh!

not sure if this is relevant anymore, but i found the table class i was talking about: qiktable

is it because objects in php4 don’t work like you think?
http://www.obdev.at/developers/articles/00002.html

Care to Comment?

Or if you'd prefer to get in touch privately, please send me an email.

Name

Email (optional)

Blog (optional)