Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

[web] Separating html from code

Started by Michael Tanczos Jun 29, 2004 at 3:18 AM 15 replies 3.7k views
Original Post
Michael Tanczos
Michael Tanczos
One of the biggest problems with a number of web scripting languages is how intermixed code and html can become. In the course of developing these forums I've managed to successfully implement a fairly good separation of the two. With most web development you're going to find that you'll be pulling some sort of data from a database and outputting HTML so they end user can view it with their favorite browser. It becomes problematic, particularly with compiled languages, to co-mingle regular code and the actual html output. I've worked on projects in the past where the process was: 1) Change HTML code 2) Compile 3) See how it looks 4) Go to 1 Luckily a very elegant solution exists in the form of XML. A number of databases today are now building in ways to pull data from a database directly in XML format. Even without that functionality, it is easy to reshape the results of a database query into an XML document.

<xml>
  <messages>
     <message>
        <id>1052</id>
        <topic>Separating html from code</topic>
     </message>
     <message>
        <id>1053</id>
        <topic>Cooking with Gas</topic>
     </message>
  </messages>
</xml>
Now XML is just another way of representing the same data you would pull from a database. So purely from a performance standpoint, we don't see any real advantages to making this conversion. What we ultimately want is a way to transform the XML into a nice, pretty format the end user can appreciate. The answer to our prayers comes in the form of a technology called XSLT. This particular technology allows us to create a template specifying precisely how we want to display the data within the XML node set. Example XSL document:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>

   <xsl:template match="/">
        <h1>Topic</h1>
        <table cellpadding="3" cellspacing="0">
            <tr>
                <th>Message</th>
            </tr>
            <xsl:apply-templates select="//message"/>
        </table>
    </xsl:template>
    <xsl:template match="message">
        <tr>
            <td><xsl:value-of select="id" /></td>
        </tr>
	<tr>
	    <td><xsl:value-of select="topic" /></td>
	</tr>
    </xsl:template>
</xsl:stylesheet>
The general approach you would exercise to utilize XSL would be: 1. Perform database query 2. Convert to XML 3. Load XSL stylesheet 4. Apply XSL transformation to XML 5. Display resulting output Utilizing XSL documents gives you something of a "template" language that can work with your existing compiled or scripted code. If you want examples of where we use XSL.. the forum listing and topic listing page you're looking at now are generated from XSL. The journal section and even the comments there are XSL generated. The application of XSL to an XML nodeset is language-specific. But sometimes awareness of what is available is more important than specific knowledge of how to do something. There are plenty of resources freely available that will show you how to perform the steps I just mentioned. =) --- Michael Tanczos
evolutional
evolutional
If I'm not mistaken, Microsoft have based pretty much all of their ASP.NET web components on exactly this technique you talk about.

XSLT is particulary powerful in 'classic' ASP where (aside from Server.Execute and SSI hacks) you have no real ways of keeping the code separate from the HTML output. You undoubtedly know (being that GameDev looks like it's using Classic ASP) that this can become a nightmare for site maintainence so XSLT transformations will help releive this somewhat.

I'd be interested in sharing your links (I've spent up on Books this month :/) - especially the translation of database queries into XML. Do you use an intermediate file, or the MSXML component?

In my experience, WebMonkey and SiteExperts are useful references for such things. Don't forget ASP.net which is supported by Microsoft.

I know ASP has XML components, but do PHP programmers have the same luxury?

[Edited by - evolutional on June 29, 2004 4:34:55 AM]
seanw
seanw
Quote:
Original post by evolutional
I know ASP has XML components, but do PHP programmers have the same luxury?


One good reason to represent your data in XML is that there are XML libraries available for all major programming languages that provide pre-made tools for you to use that you couldn't if you decided to use your own custom format. PHP certaintly has XML and XSLT modules available for it.
evolutional
evolutional
I just googled and came across good ol' faithful 4guysfromrolla and they have a nice tutorial on using MSXML 4.0 to build an XML file from a recordset. Clicky

I'm going to have a play on my server, this way I get the power of MySQL and the niceness of XML. Thanks Mr Tanczos ;)
Michael Tanczos
Michael Tanczos
There are some really nice OSS XML/XSL libraries out there as well. I've found it to be particularly annoying to program something in C++ (particularly when you don't want to give the source away as with ASP or PHP) and have to hard-code the HTML inside it.

With classic ASP I actually utilized MSXML. One reason I did this in particular was that through ASP I could even expose vbscript functions for use inside the XSL sheets. But I must admit, XSL is pretty powerful without extensions.

I think Planet Source Code is pretty invaluable.. even if only to get ideas. For ASP I like Aspin.com. For pretty much any scripting language, Hot Scripts is excellent.

---
Michael Tanczos
evolutional
evolutional
I just ported my basic news page into XML/XSLT in about 30 minutes. It now follows this process:

1) Query MySQL Database
2) Build XML document with MSXML 4
3) Transform using news.xsl
4) Display page

It's nice because the XSL can be changed and the layout of the entire newslist will be altered. The newslist is created as a separate asp script which is then executed in place using Server.Execute. I especially like the newfound ability to export my newsfeed as an XML file and with a bit of tinkering, I'm guessing I could build an RSS feed from it.

The only drawback I can see is that if my newsitems included any HTML tags (eg: a / img / etc ) these then become mangled in the translation process to lt / gt pairs. Any ideas on how to stop this (other than a Replace() call as I'm doing now)?
seanw
seanw
Quote:
Original post by evolutional
The only drawback I can see is that if my newsitems included any HTML tags (eg: a / img / etc ) these then become mangled in the translation process to lt / gt pairs. Any ideas on how to stop this (other than a Replace() call as I'm doing now)?


Create your own custom XML tag for indicating images then use the XSLT to replace this tag with the HTML "img" tag.
Michael Tanczos
Michael Tanczos
Yes, for each tag you want to have output as html (e.g. non-escaped) add the following attribute/value pair:

disable-output-escaping="yes"

So you would use it like this:




---
Michael Tanczos
Kylotan
Kylotan
Quote:
Original post by Michael Tanczos
One of the biggest problems with a number of web scripting languages is how intermixed code and html can become. In the course of developing these forums I've managed to successfully implement a fairly good separation of the two.


In the interests of starting a discussion on this, did you really find it to be a problem as such? Personally I'm used to it and accept the mixture as something you can't get around. Every C printf statement has a mini-language for the format string, every Perl regular expression has a mini-language for the expression, and I don't think embedding the HTML in the PHP or ASP code is any different. This is especially true when most of the presentation data is hidden away in the CSS.

Here's how my mod_python setup might handle your example, roughly speaking:
messages = databasequery("SELECT blah blah")output += '<h1>Topic</h1>'output += '<table cellpadding="3" cellspacing="0">'output += '<tr><th>Message</th></tr>'for message in messages:    output += "<tr><td>%s</td></tr>" % message['id']    output += "<tr><td>%s</td></tr>" % message['topic']output += '</table>'


Yes, technically the HTML is mixed in with the Python. But it only plays the role of a template anyway. I wasn't convinced when I used XSLT that it made anything any more maintainable than the pretty clean code above. The above is not optimised for either speed or brevity, I should add.


As an aside, here's the version of the above that is optimised for brevity:
output += """<h1>Topic</h1><table cellpadding="3" cellspacing="0"><tr><th>Message</th></tr>""" + "".join(["<tr><td>%s</td></tr><tr><td>%s</td></tr>" % (message['id'], message['topic'])for message in databasequery("SELECT blah blah")]) + "</table>"


Ugly, isn't it? :)
Michael Tanczos
Michael Tanczos
The problem is more apparent with compiled code. In a sense all XSLT provides for you is something of a template langauge for shaping a data set. To some degree you are still programming it when you create it.

I don't find the co-mingling inherently bad per se, but I find it to be more sloppy than solutions which can separate the two. I think it leaves a lot of room to change the backend without dealing with extra html code.. it also allows you to test new interfaces without having to make copies of the same script or take the site offline.

---
Michael Tanczos
teamonkey
teamonkey
Quote:
Original post by Michael Tanczos
XML stuff


That's kinda cool, though I can't really see any practical advantage over:

<? while($message = mysql_fetch_row($query)) { ?>< h1 >Topic</ h1 ><table cellpadding="3" cellspacing="0"><tr>    <th>Message</th></tr><tr>    <td><?=$message['id']?></td></tr><tr>    <td><?=$message['topic']?></td></tr></table><? } ?>


Which does exactly the same thing, but is much more readable in, for example, Dreamweaver? Maybe it's more useful for ASP.
[teamonkey] [blog] [tinyminions]
Sir Valeq
Sir Valeq
Does xsl work well with Netscape (7.1)?
It seems to be interpreted well in IE6, but fails in Netscape...
--------------"We cannot all be masters." - William Shakespeare
evolutional
evolutional
Depending on your server set up, XSL/XML parsing is done server-side, so the client should receive only the resulting HTML pages.
igni ferroque
igni ferroque
I find it interesting that so far nobody has mentioned Apache Cocoon, which is probably one of the most powerful XML publishing frameworks available. Cocoon uses a pipeline system, where XML data is read, transformed as necessary, and then is sent to the client. It can be used to generate HTML, XHTML, SVG, JPEG, PNG, PDF (via XSL Formatting Objects), SWF, or pretty much any format imagineable. It also allows for advanced form handling, flow control, and internationalization.

One of its greatest advantages is its advanced data caching system. Data sources (XML files, database query results, etc.) can be cached, as are precompiled XSL stylesheets and the final output if possible, thus reducing the overhead for popular requests. With a couple XML tags you can pass modern web browsers the raw XML data and the appropriate stylesheets and let them render the data, further reducing overhead.

Oh, and it supports PHP for data generation if you have the PHP Java servlet.
Free Mac Mini (I know, I'm a tool)
00squeaky
00squeaky
well, that is great and all, but the problem is not alot of people understand nor want to learn XML.

i'd much rather suggest downloading an apache or abyss web server, so the process you defined would not only go alot faster, but no one else will be able to see your blunders ;)
00squeaky at your service...maybe.
igni ferroque
igni ferroque
Quote:
Original post by 00squeaky
well, that is great and all, but the problem is not alot of people understand nor want to learn XML.

i'd much rather suggest downloading an apache or abyss web server, so the process you defined would not only go alot faster, but no one else will be able to see your blunders ;)


What are you talking about?
Free Mac Mini (I know, I'm a tool)
evolutional
evolutional
I've just built my first real XHTML/CSS/XML/XSLT based web page.

In fact, I've ported my two main sites over to this idea and added a new one for jsInvaders. I'm really pleased in how it's allowing me to work.

Thanks for the prompting, Michael Tanczos

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.