changeset 32:4cea8b128fdd

Possibilité de choisir des templates différents en fonction du type de sortie. Le fichier templates.conf permet d'associer à chaque type de sortie (correspondant à une section du fichier INI) des templates pour le squelette, les posts et les commentaires. Il est possible de définir virtuellement une infinité de type de sorties différents, pour le moment seul de quoi obtenir un flux Atom a été ajouté. Pour changer le type de sortie, il faut passer en GET au script appellant un paramètre outputType. Si le paramètre reçu ne correspondant à aucun des types de sorties définis dans templates.conf, alors on prend du xhtml par défaut.
author Franck Deroche <webmaster@defr.org>
date Mon, 05 Nov 2007 09:06:46 +0100
parents 2d98421d43da
children c6508503269a
files comment.php includes/class.post.php includes/class.skeleton.php includes/templates.conf templates/atom.xml templates/commentAtom.xml templates/postAtom.xml
diffstat 7 files changed, 99 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/comment.php	Mon Nov 05 08:59:51 2007 +0100
+++ b/comment.php	Mon Nov 05 09:06:46 2007 +0100
@@ -47,7 +47,7 @@
       $Data->Query("SELECT * FROM Commentaires Where MessId={$id} AND Visible=1 ORDER BY num_comm");
       $defaultGravatar = urlencode("http://defr.org/Misc/NoGravatar.png");
       while(0 !== ($row = $Data->GetRow())) {
-	    $commentTpl = new Template('comment.xml');
+	    $commentTpl = new Template(Skeleton::getTemplateFile('comment'));
         $dateFormatee = strftime(" à %Hh%M le <span class='Date'>%A %d %B %Y</span>", strtotime($row['DateComment']));
         $AdresseMail = $row['AdresseMail'];
         $grav_id = md5($AdresseMail);
@@ -62,12 +62,13 @@
 		  '#auteur@name' => 'c' . $row['num_comm'],
 		  '#auteur' => $row['Auteur'],
 		  'commentDate' => $dateFormatee,
+		  'commentNumber' => $row['num_comm'],
 		  'comment' => $commentFrag,
 	    );
         if(stristr($row['Adresse'], 'http://')) {
             $params['#auteur@href'] = $row['Adresse'];
         }
-        $commentTpl->setParams($params);
+        $commentTpl->setParams(array_merge($post->getTplParams(), $params));
         $sk->addBlogPost($commentTpl);
       }
 
--- a/includes/class.post.php	Mon Nov 05 08:59:51 2007 +0100
+++ b/includes/class.post.php	Mon Nov 05 09:06:46 2007 +0100
@@ -12,8 +12,7 @@
   public function __construct($infos) {
     $this->infos = (object)$infos;
 
-    // Récupération du template
-    $this->tpl = new Template('post.xml');
+    $this->tpl = new Template(Skeleton::getTemplateFile('post'));
 
     // Création du document fragment contenant le message
     $Mess = str_replace(
--- a/includes/class.skeleton.php	Mon Nov 05 08:59:51 2007 +0100
+++ b/includes/class.skeleton.php	Mon Nov 05 09:06:46 2007 +0100
@@ -8,8 +8,15 @@
   private $calendarYear;
   private $useXML;
 
+  const OUTPUT_XHTML = 'xhtml';
+  const OUTPUT_ATOM  = 'atom';
+  const OUTPUT_RSS   = 'rss';
+
+  private static $outputType = OUTPUT_XHTML;
+  private static $templates = array();
+
   public function __construct() {
-     parent::__construct('main.xml');
+     parent::__construct(Skeleton::getTemplateFile('skeleton'));
      parent::shouldStripAttributesInTemplateNS(true);
      $this->styleSheets = array();
      $this->addDefaultSheets();
@@ -216,5 +223,33 @@
       $returnValue = ereg_replace('<\?xml[^\?]*\?>', '', $returnValue);
     return $returnValue;
   }
+
+  public static function getTemplateFile($type) {
+    if(count(Skeleton::$templates) == 0) {
+      // Construction du tableau des différents templates
+      // permettant de répondre au "type" de sortie demandé
+      $iniFile = parse_ini_file('templates.conf', true);
+     
+      $format = Factory::getRequete()
+                       ->get('outputType', null, Skeleton::OUTPUT_XHTML);
+
+      if(array_key_exists($format, $iniFile)) {
+        Skeleton::$templates = $iniFile[$format];
+        Skeleton::$outputType = $format;
+      } else {
+        Skeleton::$templates = $iniFile[Skeleton::OUTPUT_XHTML];
+        Skeleton::$outputType = Skeleton::OUTPUT_XHTML;
+      }
+    }
+    return Skeleton::$templates[$type];
+  }
+
+  public static function getOutputType() {
+    // On s'assure que la demande a été analysée, si ce n'est pas
+    // le cas on le fait maintenant, en demandant un template
+    if(count(Skeleton::$templates) == 0)
+        Skeleton::getTemplateFile('skeleton');
+    return Skeleton::$outputType;
+  }
 }
 ?>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/includes/templates.conf	Mon Nov 05 09:06:46 2007 +0100
@@ -0,0 +1,9 @@
+[xhtml]
+skeleton = main.xml
+post = post.xml
+comment = comment.xml
+
+[atom]
+skeleton = atom.xml
+post = postAtom.xml
+comment = commentAtom.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/atom.xml	Mon Nov 05 09:06:46 2007 +0100
@@ -0,0 +1,17 @@
+<feed xmlns="http://www.w3.org/2005/Atom" 
+      xmlns:tr='http://defr.net/2007/template'
+      xml:lang='fr-FR'>
+  <title>Dual Blog</title>
+  <subtitle>Le blog de DeFr, parlant de tout et de rien ^^;</subtitle>
+  <link rel='alternate' type='text/html' href='http://defr.org/blog/' />
+  <link rel='self' type='application/atom+xml' href='http://defr.org/blog/dualblog.atom' />
+  <updated><tr:modifiedDate /></updated>
+  <author>
+   <name>Deroche Franck</name>
+   <uri>http://defr.org</uri>
+  </author>
+  <id>urn:uuid:cface534-634f-4ce8-a07b-3a4187982583</id>
+  <tr:wrapper tr:id='Posts'>
+      <tr:post tr:toClone='true' />
+  </tr:wrapper>
+</feed>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/commentAtom.xml	Mon Nov 05 09:06:46 2007 +0100
@@ -0,0 +1,16 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<entry xmlns='http://www.w3.org/2005/Atom'
+       xmlns:tr='http://defr.net/2007/template'>
+   <title type='text'>
+       <tr:postTitle /> - <tr:wrapper tr:id='auteur' />
+   </title>
+   <id>tag:defr.org,comment,2006:<tr:commentNumber /></id>
+   <content type='xhtml'>
+   	<div xmlns='http://www.w3.org/1999/xhtml'>
+	  <tr:comment />
+	</div>
+   </content>
+   <published><tr:dateComment /></published>
+   <updated><tr:dateComment /></updated>
+   <link tr:id='linkPostNumber' rel='alternate' type='text/html' />
+</entry>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/postAtom.xml	Mon Nov 05 09:06:46 2007 +0100
@@ -0,0 +1,17 @@
+<entry xmlns='http://www.w3.org/2005/Atom'
+       xmlns:tr='http://defr.net/2007/template'>
+   <title type='xhtml'>
+     <div xmlns='http://www.w3.org/1999/xhtml'>
+       <tr:postTitle />
+     </div>
+   </title>
+   <id>tag:defr.org,2006:<tr:postNumber /></id>
+   <content type='xhtml'>
+   	<div xmlns='http://www.w3.org/1999/xhtml'>
+	  <tr:postContent />
+	</div>
+   </content>
+   <published><tr:postDate /></published>
+   <updated><tr:postDate /></updated>
+   <link tr:id='linkPostNumber' rel='alternate' type='text/html' />
+</entry>