In questa pagina cercherò di spiegare il funzionamento della classe Template, basata su XSLT.
Più che una guida vera e propria, questa pagina, sarà una raccolta di esempi che verranno aggiunti a mano a mano qualora lo si riterrà necessario.
web/
index.php
lib/
Template.php
views/
index.xsl
<?php
require 'library/Template.php';
$view = new Template();
// Assegno bar alla variabile foo
$view->assign('foo', 'bar');
// Sintassi alternativa
$view->title = 'Example';
if (!isset($_GET['debug'])) {
// Visualizzo il risultato
echo $view->render('views/index.xsl');
} else {
// Oppure visualizzo l'albero xml (debug)
echo $view->render();
}
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Formattazione output -->
<xsl:output
method="xml"
omit-xml-declaration="yes"
encoding="utf-8"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
indent="yes"
media-type="text/html"/>
<!-- Punto d'entrata -->
<xsl:template name="general" match="/">
<html xml:lang="it" lang="it">
<head>
<title><xsl:value-of select="title"/></title>
</head>
<body>
<!-- Richiamo la variabile -->
<p><xsl:value-of select="foo"/></p>
</body>
</html>
</xsl:template>
</xsl:transform>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="it" lang="it">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example</title>
</head>
<body>
<p>bar</p>
</body>
</html>