Page 1 of 2

OOP PHP DOM Abstraction? Layer

Posted: Thu Aug 31, 2006 12:12 am
by Luke
Sorry for all the abbreviations... my question is... has anybody every tried to create a PHP library that would be basically an abstraction (is that the correct term) layer to the Document Object Model... so basically you would never write any html... you would create php objects for each html tag you need. So basically the same kind of concept as DOM nodes in javascript, but in php... does anybody see what I am getting at?

Posted: Thu Aug 31, 2006 12:16 am
by feyd

Posted: Thu Aug 31, 2006 12:24 am
by Luke
Umm... yea pretty much. Do you use that at all? I haven't had time to look it over completely, but I imagine this could be used to compose fully valid XHTML documents? This could be a pretty powerful set of objects if put in the right hands it seems like... I wonder how come I've never seen/heard of it before... :?

Posted: Thu Aug 31, 2006 12:31 am
by feyd
No, I don't use it currently. Although I'm not really meddling in the display code right now.

I imagine it's not getting much use because, like with Javascript, people choose the shortcut route (inline text literals and innerHTML for example) or choose preformed templates or other functionality (XSLT for example.)

I would certainly prefer generating output via the DOM when the output is XML/HTML based, but my output isn't always so simple. Sometimes it's PDF based, XUL or even command line based. All can need some adjustments of how controls are integrated, so it's difficult in my case to justify using the DOM a lot.

Posted: Thu Aug 31, 2006 12:38 am
by Luke
To me it seems that this could easily be extended to dynamically create very richly featured and well written javascript/php documents that are guaranteed to validate. I am going to start messing around with it. I was considering writing a PHP interface to the Google Maps API, and I think this could really help me out. It seems perfect for this task.

Posted: Thu Aug 31, 2006 12:57 am
by alex.barylski
Edit: You'd have a helluva time using the DOM to re-write javascript ;)

I've researched just this...

My conclusion is, using a DOM would be a fantastic way of doing it as you *could* output valid XHTML, etc...

The problem is, your starting to work backwards...if you read up on the history of temlate engines, you'll see what I mean...

HTML has it's power in being a self-driven GUI...meaning there is no programming required to get things to display...

PHP allows for quick adhock hacks and is why you often see PHP intermingled with HTML...but in theory they can both be used seperately...allowing for optimal seperation of presentation from logic...

Realistically of course, we need template logic...at least sometimes...so it's not perfect...

Working with a DOM does two counter evolutionary things:
1) Removes the need for a HTML hack and brings the UI burden back onto a programmer
2) Brings display back into the domain of "programming"

These two things are what HTML (although not originally designed to do) is basically doing to the world of application development...

So although the DOM is cool...yes I have plenty of experience in working with DOM's I know it's neat...I'm not sure it would make for a good replacement for template engines or hardcoding HTML...

A couple years back I discovered this very same thing...but in javascript...I used the DOM to re-write the crappy HTML IE output into nice xhtml (or what I thought was xhtml)...when other developers where using regex and nasty hacks to re-write the xhtml...

now it's common practice amongst most WYSIWYG editors...TInyMCE uses this aproach from what I can tell...

http://html2xhtml.richarea.com/

He does too :P

Posted: Thu Aug 31, 2006 1:06 am
by Luke
I wouldn't use it to rewrite javascript... I would use it to allow very easy integration with pre-built javascript libraries such as Google Maps ($DOMElement->attachGoogleMap()), and scriptaculous ($DOMElement->makeDraggable()) See what I mean? I think this would be EXTREMELY cool.

Posted: Thu Aug 31, 2006 1:17 am
by daedalus__
/agree

Though I think that this could turn in to a mountain of a project.

Posted: Thu Aug 31, 2006 1:43 am
by alex.barylski
Not sure I see where your coming from...

You want to use the DOM to insert javascript, etc into the templates, by basically:

Code: Select all

getElementById('jscript').innerHTML = "
  alert('THis is uber cool javascript?');
";
Instead of using regex and replacing placeholders???

Implement something and I'll comment then as I still don't clearly understand what it is your doing... :oops:

I love DOM though so keep the topic rolling if you want ;)

Posted: Thu Aug 31, 2006 4:39 am
by volka
I think we're talking about something like

Code: Select all

$doc = new DOMDocument('1.0', 'iso-8859-1');
$doc->appendChild($doc->createProcessingInstruction('DOCTYPE', [...]));
[...]
$eScript = $doc->createElement('script');
$eScript->setAttribute('type', 'text/javascript');
$eScript->appendChild($doc->createCDATASection($javascript));
[...]

Posted: Thu Aug 31, 2006 9:57 am
by Luke
volka's got the basic idea

Posted: Thu Aug 31, 2006 9:59 am
by alex.barylski
Thats exactly what I did, only syntactically and more technically correct...but the gist of the idea is exactly the same :P

Posted: Thu Aug 31, 2006 1:14 pm
by Luke
I can kind of agree about this giving too much responsibility to the developer, but for an all-in-one type guy like myself it seems to make sense to create documents like this... and also, it looks like you can import in already created xhtml docs and it will convert them... so you could have somebody design a template, and then you take over from there, right?

Posted: Thu Aug 31, 2006 8:59 pm
by alex.barylski
Yes...there is a template engine that uses the DOM allowing the developer to provide all the manipulation desireable...but I cannot for love nor money find it...

I think it was a link under wikipedia as a external resource for Smarty???

I'll keep looking if I can find it i'll post it

Posted: Thu Aug 31, 2006 10:44 pm
by alvinphp
The Ninja Space Goat wrote:Umm... yea pretty much. Do you use that at all? I haven't had time to look it over completely, but I imagine this could be used to compose fully valid XHTML documents? This could be a pretty powerful set of objects if put in the right hands it seems like... I wonder how come I've never seen/heard of it before... :?
Why not just code the site in XHTML? I am kind of confused at the point of this. All the sites I do now are in XHTML.