One of the mistakes many new PHP developers make is not separating their code layer from their presentation layer.
When creating applications for the web, for instance, new developers will often mix backend database calls and other programming logic with HTML. For small applications, this isn't usually a problem, but for bigger projects this can make the code unreadable and hard to maintain for other developers who might make future additions to the project. What is needed is a way to separate the backend (PHP) from the front-end (HTML) that won't complicate the development process.
In this tutorial, we will explore one way of accomplishing this separation: XSLT.
What is XSLT?
The World Wide Web Consortium (WC3) defines the Extensible Stylesheet Language Transformations (XSLT) as "a language for transforming XML documents into other XML documents." This is very clear, right? Think of it as a way to change or control XML or document output.
With XSLT, developers can create custom XML documents that will, in turn, be transformed into HTML. XSLT sits between the code layer and the presentation layer, allowing the two layers to be completely separated. Developers can now concentrate on writing code and designers can concentrate on creating the user interface, without stepping on each other’s toes.
How does XSLT work?
Developers focused on the backend of an application typically use business logic to work with content that is stored in a database. For applications that use XSLT, the backend developer will take this content and arrange it into an XML document.
<widgets>
<widget>
<id>123456789</id>
<name>widget1</name>
</widget>
<widget>
<id>987654321</id>
<name>widget2</name>
</widget>
</widgets>
Developers who are focused on the front-end of an application would then take this XML document and apply it to an XSLT stylesheet to create the HTML output.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1" indent="no"/>
<xsl:template match="widgets">
<p>Here are our fabulous widgets.</p>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="widget">
<h1><xsl:value-of select="id"/></h1>
<h2><xsl:value-of select="name"/></h2> <hr />
</xsl:template>
</xsl:stylesheet>
The HTML output would then be something like this.
<p>Here are our fabulous widgets.</p>
<h1>123456789</h1>
<h2>widget1</h2>
<hr/>
<h1>987654321</h1>
<h2>widget2</h2>
<hr/>
How to Use XSLT with PHP
Transforming an XML document with PHP is easy. First, you will need to create two instances of the DOMDocument class to hold both the XSLT stylesheet and the XML document you want to transform.
$xsl = new DOMDocument();
$xsl->load("stylesheet.xsl");
$xml = new DOMDocument();
$xml->load("widgets.xml");
Next, create an instance of XSLTProcessor to import the XSLT stylesheet.
$proc = new XSLTProcessor();
$proc->importStylesheet($xsl);
Finally, call the 'transformToXML' method to create the HTML output. Note, the output needs to be valid XHTML or you will get errors.
echo $proc->transformToXML($xml);
That's it. You’ve just used XSLT and PHP to keep your project’s backend and front-end neatly organized.
Conclusion
Separating the backend code from the presentation layer is a practice that has become an industry standard. By doing so, you end up with an application that is both easy to understand and easy to maintain. XSLT is a WC3 standard, well documented, and it has an enormous user base, so you will never feel alone when developing with it.
