kevinleary.net

True Seperation of Presentation & Content with XML

Web stan­dards books praise the idea of sep­a­rat­ing pre­sen­ta­tion from con­tent. Unfor­tu­nately doing so is harder said than done in the pro­fes­sional world. When I begin work­ing on a re-design I usu­ally find myself spend­ing count­less hours re-formatting con­tent, so what’s the point? If a client wants spe­cific infor­ma­tion laid out as an unordered list (or out­line) one day and tab­u­lar data the next you’ll find your­self rebuild­ing the pages structure.

The best solu­tion to this ongo­ing prob­a­bly is to uti­lize XML to describe the con­tent as best as pos­si­ble. The one prob­lem with this approach is that pars­ing that XML into usable code is usu­ally a big pain in the ass. With PHP5’s imple­men­ta­tion of the Sim­pleXML object pars­ing XML is finally as sim­ple as it should be.

This is the best way to han­dle web con­tent in my opin­ion. The advan­tages as I see them are:

  • Store your con­tent in one cen­tral loca­tion, even it is used in mul­ti­ple places through­out a site
  • Write your con­tent once using the most seman­tic markup of all, XML
  • Loop through tags to make styling repeated con­tent a breeze (e.g. Press Releases page)
  • Use your con­tent with dif­fer­ent tech­nolo­gies. Cre­ate RSS feeds, sup­ply it to other domains or use it with Flash

Using the Sim­pleXML object

Let’s get our hands dirty and see how to parse an XML file with SimpleXML.

XML

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8" ?>
<press>
	<article>
		<date>August 26</date>
		<year>2008</year>
		<link>pdf/consumer-brochure.pdf</link>
		<title>New press release!</title>
		<description>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua...</description>
	</article>
</press>

PHP

1
2
3
4
5
6
7
8
9
10
<?php 
$press = simpleXML_load_file("press.xml"); 
foreach($press as $articles) { 
?>
<div class="article">
	<h2><a href="<?= $articles->link ?>"><?= $articles->title ?></a></h2>
	<p><?= $articles->description ?></p>
	<p class="date"><strong>Published</strong> <?= $articles->date ?>, <?= $articles->year ?></p>
</div>
<? } ?>

XHTML (gen­er­ated by SimpleXML)

1
2
3
4
5
<div class="article">
	<h2><a href="pdf/consumer-brochure.pdf">New press release!</a></h2>
	<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua...</p>
	<p class="date"><strong>Published</strong> August 26, 2008</p>
</div>

Down­load source

Down­load the Sim­pleXML Source Code for the exam­ple above.

Rec­om­mended Reading

I was intro­duced to the Sim­pleXML object while brows­ing PHP 5 Recipes: A Problem-Solution Approach True Seperation of Presentation & Content with XML. I would rec­om­mend this book to pro­fes­sional devel­op­ers look­ing for a great desk ref­er­ence resource. As best put by the author, “This book is a source of instant solu­tions, includ­ing count­less pieces of use­ful code that you can just copy and paste into your own appli­ca­tions, giv­ing you answers fast and sav­ing you hours of cod­ing time.” That said, it’s big, long, and def­i­nitely not some­thing you sit down and actu­ally read. It pro­vides a well doc­u­mented ref­er­ence of PHP 5 that includes some of the newer addi­tions such as SimpleXML.

Resources

2 Comments

  1. Sasa Bogdanovic / 8.30.08 / 4:40 PM

    Well I guess you have a typo in the arti­cle title. It should have read sep­a­ra­tion of pre­sen­ta­tion and con­tent, not contact :)

  2. admin / 9.1.08 / 12:24 PM

    Good catch, thanks. I prob­a­bly would never have noticed

Leave a comment

will not be published

Wrap code blocks with <pre lang="LANGUAGE" line="1"> and </pre> where LANGUAGE is a GeSHi supported language syntax.