cvs: peardoc /fr/package/html html-template-phplib.xml

From: Date: Wed, 01 Nov 2006 23:09:05 +0000
Subject: cvs: peardoc /fr/package/html html-template-phplib.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-8696@lists.php.net to get a copy of this message
moosh Wed Nov 1 23:09:05 2006 UTC Added files: /peardoc/fr/package/html html-template-phplib.xml Log: - begin translation

http://cvs.php.net/viewvc.cgi/peardoc/fr/package/html/html-template-phplib.xml?view=markup&rev=1.1 Index: peardoc/fr/package/html/html-template-phplib.xml +++ peardoc/fr/package/html/html-template-phplib.xml <?xml version="1.0" encoding="iso-8859-1" ?> <!-- $Revision: 1.1 $ --> <!-- EN-Revision: 1.1 Maintainer: moosh Status: started --> <sect1 id="package.html.html-template-phplib"> <title>HTML_Template_PHPLIB</title> <para> <classname>HTML_Template_PHPLIB</classname> est une version PEARisée de la fameuse classe de template PHPLIB. It provides variables and repeatable blocks. Le code original de PHPLIB peut-être trouvé sur <ulink url="http://phplib.sourceforge.net/">phplib.sf.net</ulink>. </para> <sect2> <title>Concept</title> <para> Quand vous utilisez <classname>HTML_Template_PHPLIB</classname> pour générér du HTML ou un codre code depuis des modèles, vous devez suivre les étapes suivantes: <orderedlist> <listitem><para>Créer une instance de la classe template</para></listitem> <listitem><para>Charger le fichier template</para></listitem> <listitem><para>Définir les blocs</para></listitem> <listitem><para>Setter les variables, parser les blocs</para></listitem> <listitem><para>Terminer et afficher</para></listitem> </orderedlist> </para> <para> Les <emphasis>Variables</emphasis> sont des placeholders dans votre code HTML qui pourront être remplacée dynamiquement par valeurs venant d'une base de donnée ou calculées dans votre code. Voici un exemple de variable <literal>{CODE_AUTHOR}</literal>: Vous entourez le nom de variable d'accolades. Le nom de la variable peut contenir n'importe quel caractères autres que les "blancs". (espace, tabulation, retour à la ligne) </para> <para> Les <emphasis>Blocs</emphasis> mettent en évidence des morceau de code HTML et peut être réutilisé, p.e. une ligne <literal>&lt;tr&gt;</literal> dans une table. Les blocs sont définis en utilisant des commentaires HTML contenant <literal>BEGIN</literal> ou <literal>END</literal>, et le nom du bloc. Example: <programlisting role="html"><![CDATA[ <table> <caption>Authors</caption> <thead> <tr><th>Name</th><th>Email</th></tr> </thead> <tbody> <!-- BEGIN authorline --> <tr><td>{AUTHOR_NAME}</td><td>{AUTHOR_EMAIL}</td></tr> <!-- END authorline --> </tbody> </table> ]]></programlisting> </para> </sect2> <sect2> <title>Exemple simple</title> <example> <title>Fichier template: <filename>authors.tpl</filename></title> <programlisting role="html"><![CDATA[ <html> <head><title>{PAGE_TITLE}</title></head> <body> <table> <caption>Authors</caption> <thead> <tr><th>Name</th><th>Email</th></tr> </thead> <tfoot> <tr><td colspan="2">{NUM_AUTHORS}</td></tr> </tfoot> <tbody> <!-- BEGIN authorline --> <tr><td>{AUTHOR_NAME}</td><td>{AUTHOR_EMAIL}</td></tr> <!-- END authorline --> </tbody> </table> </body> </html> ]]></programlisting> </example> <example> <title>Code PHP : <filename>authors.php</filename></title> <programlisting role="php"><![CDATA[ <?php //we want to display this author list $authors = array( 'Christian Weiske' => 'cweiske@php.net', 'Bjoern Schotte' => 'schotte@mayflower.de' ); require_once 'HTML/Template/PHPLIB.php'; //create template object $t =& new HTML_Template_PHPLIB(dirname(__FILE__), 'keep'); //load file $t->setFile('authors', 'authors.tpl'); //set block $t->setBlock('authors', 'authorline', 'authorline_ref'); //set some variables $t->setVar('NUM_AUTHORS', count($authors)); $t->setVar('PAGE_TITLE', 'Code authors as of ' . date('Y-m-d')); //display the authors foreach ($authors as $name => $email) { $t->setVar('AUTHOR_NAME', $name); $t->setVar('AUTHOR_EMAIL', $email); $t->parse('authorline_ref', 'authorline', true); } //finish and echo echo $t->finish($t->parse('OUT', 'authors')); ?> ]]></programlisting> </example> </sect2> <sect2> <title>Charger les fichiers</title> <para> Les fichiers template sont relatif à un dossier <emphasis>root</emphasis> configurable. You can specify it directly in the constructor (first parameter, defaults to <literal>.</literal>) or via <function>setRoot</function>. </para> <para> Files are referenced via <emphasis>handle</emphasis> names. Thus you need to specify the handle name as first parameter, and the file's name as second parameter when calling <function>setFile</function>. To speed things up, you may pass an array of handle-filename pairs to the method. </para> <example> <title>Loading a template file</title> <programlisting role="php"><![CDATA[ $t =& new HTML_Template_PHPLIB(dirname(__FILE__), 'keep'); $t->setFile('authors', 'authors.tpl'); ]]></programlisting> </example> </sect2> <sect2> <title>Définir les blocs</title> <para> Si vous voulez réutiliser certaines partie de votre code HTML plusieurs fois, par exemple les lignes d'un tableau, vous pouvez définir des blocs comme dans l'exemple décris ci-dessus. </para> <para> Les blocs ne sont pas automatiquement détectés, ils doivent être explicitement défini via la méthode <function>setBlock</function>. The method takes the handle of the file in which the block is located as first parameter, the name of the block as written in the file as second and the new <emphasis>block handle</emphasis> as third parameter. </para> <example> <title>Defining a block</title> <programlisting role="php"><![CDATA[ $t->setBlock('authors', 'authorline', 'authorline_ref'); ]]></programlisting> </example> <para> In the example template (available as handle <literal>authors</literal>, a block named <literal>authorline</literal> is defined. The handle of the block is now <literal>authorline_ref</literal>. </para> </sect2> <sect2> <title>Setting variables</title> <para> This is the most easy part: Just use <function>setVar</function> with the variable name and it's new value. An optional third boolean parameter tells if the value should be appended to the existing value (defaults to false). </para> <example> <title>Setting a variable</title> <programlisting role="php"><![CDATA[ $t->setVar('AUTHOR_NAME', 'Christian Weiske'); ]]></programlisting> </example> <para> Variables set influence all variables in the template, no matter if they are defined within a block or not. </para> </sect2> <sect2> <title>Parsing blocks</title> <para> Once you filled all variables in a block, you want to "save" them to be able to re-use the block another time. Method <function>parse</function> is useful here. First parameter is the variable/handle in which the block content shall be stored, second is the handle name of the block. The optional third parameter tells if you want to append the block content to the handle value or not - it defaults to false, but you probably want to do that. </para> <example> <title>Parsing a block</title> <programlisting role="php"><![CDATA[ foreach ($authors as $name => $email) { $t->setVar('AUTHOR_NAME', $name); $t->setVar('AUTHOR_EMAIL', $email); $t->parse('authorline_ref', 'authorline', true); } ]]></programlisting> </example> <para> In this example, handle <literal>authorline_ref</literal> is filled with the contents of block <literal>authorline</literal>. If the third parameter would be <literal>false</literal> instead of <literal>true</literal>, only the last line would be in it at the end. </para> </sect2> <sect2> <title>Terminer &amp; afficher</title> <para> When you're done with setting variables and parsing your blocks, it's time to write the contents to your client. At first, you need to <function>parse</function> the contents of the file handle into a new handle. </para> <para> To remove all unused variables and blocks, you might want to call <function>finish</function>. Depending on the value set for <literal>unknowns</literal> (as second paramter in the constructor, or via <function>setUnknowns</function>), unused variables are either erased (<literal>remove</literal>), kept (<literal>keep</literal>) or commented out (<literal>comment</literal>). </para> <para> To echo your HTML, either directly use the return value of <function>finish</function>, or use <function>get</function> to retrieve the content. </para> <example> <title>Terminer un fichier et générer l'affichage</title> <programlisting role="php"><![CDATA[ $t->parse('OUT', 'authors'); $htmlcode = $t->finish( $tpl->get('OUT') ); echo $htmlcode; ]]></programlisting> </example> <example> <title>Terminer un fichier et générer l'affichage</title> <programlisting role="php"><![CDATA[ echo $tpl->finish($t->parse('OUT', 'authors')); ]]></programlisting> </example> </sect2> </sect1> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->
« previous php.pear.doc (#8696) next »