Defining html for direct printing and adding through javascript in one place

Hi,

I don't really know if this is the right place to ask my question, but I'll
just ask it and trust you to tell me if it's not...

First off I will start by sketching the situation and then I'll redo it with
only the required information, not all that led to me asking this question,
so you can skip this first bit if you want.
I am developing a web application and I decided to implement a very
simplistic templating system, in which there is an object that generates
most of your html when the right methods are called. By offering an object
of a different class the layout can be changed completely (it can even be
told to say 'Hello' instead of 'Welcome').
However then I started extending the application to include an ajax-driven
version of the website, still adhering to the original principle the
javascript just included parts of the html as strings generated by the
object, into which the data found in the xml response from the server could
be included in the javascript and then it all could be displayed on the page
using the proprietary, but widely supported innerHTML to display the data on
the screen in the way it was defined by the template object.
The next challenge came when I had to replace a certain div with another,
the first being part of the rendered html and the second in string format.
The problem was that I could not easily make the second string just the
contents of the of the div, it naturally included the div itself as well.
This meant I could not just use innerHTML to do this (the parent also had
other content. There are ways around this, all with their own downsides.
Wrapping the div that needs to be replaced in yet another div probably has
the least. Other options include inserting the html somewhere invisible,
then take it out of the html again as an object to place it correctly using
the DOM specification (which is just plain ugly and screams "DIRTY!"), using
insertAdjacentHTML, a brother of innerHTML (but just not as universal) and
writing my own converter to turn a string into an html element (which is
very time consuming and must be quite a strain on the client).
Along the way I wondered how I would do this in standards complient way, so
that's what I am asking here. See the brief version of the story for the
question formulated more clearly.

Only the Question:
I want to have a single place where the layout of my page is defined, which
can be used both from the server-side to create html pages and from
javascript to change an a-item into a b-item (think loginbox changing into a
panel with a logout button and user CP link). Can I do this in a manner that
is standards complient, or is that just not possible?

Thanks in advance,

Jasper Horn

Received on Monday, 16 March 2009 12:53:31 UTC