cvs: peardoc /en/package html.xml /en/package/html html-template-phplib.xml
| From: | Christian Weiske | Date: | Wed, 01 Nov 2006 15:21:47 +0000 |
| Subject: | cvs: peardoc /en/package html.xml /en/package/html html-template-phplib.xml | ||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-8694@lists.php.net to get a copy of this message | ||
cweiske Wed Nov 1 15:21:47 2006 UTC
Added files:
/peardoc/en/package/html html-template-phplib.xml
Modified files:
/peardoc/en/package html.xml
Log:
HTML_Template_PHPLIB documentation
http://cvs.php.net/viewvc.cgi/peardoc/en/package/html.xml?r1=1.21&r2=1.22&diff_format=u Index: peardoc/en/package/html.xml diff -u peardoc/en/package/html.xml:1.21 peardoc/en/package/html.xml:1.22 --- peardoc/en/package/html.xml:1.21 Tue Oct 17 20:42:05 2006 +++ peardoc/en/package/html.xml Wed Nov 1 15:21:47 2006 @@ -1,4 +1,4 @@ -<!-- $Revision: 1.21 $ --> +<!-- $Revision: 1.22 $ --> <chapter id="package.html"> <title>HTML</title> @@ -19,6 +19,7 @@ &package.html.html-table; &package.html.html-template-flexy; &package.html.html-template-it; + &package.html.html-template-phplib; &package.html.html-template-sigma; &package.html.html-template-xipe; &package.html.html-treemenu; http://cvs.php.net/viewvc.cgi/peardoc/en/package/html/html-template-phplib.xml?view=markup&rev=1.1 Index: peardoc/en/package/html/html-template-phplib.xml +++ peardoc/en/package/html/html-template-phplib.xml <!-- $Revision: 1.1 $ --> <sect1 id="package.html.html-template-phplib"> <title>HTML_Template_PHPLIB</title> <para> <classname>HTML_Template_PHPLIB</classname> is the PEARified version of the once-so-famous PHPLIB template class. It provides variables and repeatable blocks. The original PHPLIB code can be found at <ulink url="http://phplib.sourceforge.net/">phplib.sf.net</ulink>. </para> <sect2> <title>Concept</title> <para> When using <classname>HTML_Template_PHPLIB</classname> to generate HTML or other code from templates, you do the following steps: <orderedlist> <listitem><para>Create template class instance</para></listitem> <listitem><para>Load template file</para></listitem> <listitem><para>Define blocks</para></listitem> <listitem><para>Set variables, parse blocks</para></listitem> <listitem><para>Finish and output</para></listitem> </orderedlist> </para> <para> <emphasis>Variables</emphasis> are placeholders in your HTML code that can be replaced with dynamic values provided from database or calculated in your code. An example for a variable is <literal>{CODE_AUTHOR}</literal>: You enclose the variable name in curly braces. The name of the variable may contain any characters, except spaces, tabs and newlines. </para> <para> <emphasis>Blocks</emphasis> surround certain pieces of HTML code and can be re-used, e.g. a <literal><tr></literal>row in a table. Blocks are defined by using HTML comments, containing <literal>BEGIN</literal> or <literal>END</literal>, and the block name. 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>Simple example</title> <example> <title>Template file: <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>PHP code: <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>Loading files</title> <para> Template file locations are relative to a definable <emphasis>root</emphasis> directory. 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>Defining blocks</title> <para> If you want to re-use certains parts of your HTML code multiple times, e.g. rows in a table, you can define blocks as described in the example above. </para> <para> Blocks are not automatically detected, they need to be defined explicitely via the <function>setBlock</function> method. 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>Finish & output</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>Finishing a file and writing it out</title> <programlisting role="php"><![CDATA[ $t->parse('OUT', 'authors'); $htmlcode = $t->finish( $tpl->get('OUT') ); echo $htmlcode; ]]></programlisting> </example> <example> <title>Finishing a file and writing it out, the easy way</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 -->
http://cvs.php.net/viewvc.cgi/peardoc/en/package/html.xml?r1=1.21&r2=1.22&diff_format=u Index: peardoc/en/package/html.xml diff -u peardoc/en/package/html.xml:1.21 peardoc/en/package/html.xml:1.22 --- peardoc/en/package/html.xml:1.21 Tue Oct 17 20:42:05 2006 +++ peardoc/en/package/html.xml Wed Nov 1 15:21:47 2006 @@ -1,4 +1,4 @@ -<!-- $Revision: 1.21 $ --> +<!-- $Revision: 1.22 $ --> <chapter id="package.html"> <title>HTML</title> @@ -19,6 +19,7 @@ &package.html.html-table; &package.html.html-template-flexy; &package.html.html-template-it; + &package.html.html-template-phplib; &package.html.html-template-sigma; &package.html.html-template-xipe; &package.html.html-treemenu; http://cvs.php.net/viewvc.cgi/peardoc/en/package/html/html-template-phplib.xml?view=markup&rev=1.1 Index: peardoc/en/package/html/html-template-phplib.xml +++ peardoc/en/package/html/html-template-phplib.xml <!-- $Revision: 1.1 $ --> <sect1 id="package.html.html-template-phplib"> <title>HTML_Template_PHPLIB</title> <para> <classname>HTML_Template_PHPLIB</classname> is the PEARified version of the once-so-famous PHPLIB template class. It provides variables and repeatable blocks. The original PHPLIB code can be found at <ulink url="http://phplib.sourceforge.net/">phplib.sf.net</ulink>. </para> <sect2> <title>Concept</title> <para> When using <classname>HTML_Template_PHPLIB</classname> to generate HTML or other code from templates, you do the following steps: <orderedlist> <listitem><para>Create template class instance</para></listitem> <listitem><para>Load template file</para></listitem> <listitem><para>Define blocks</para></listitem> <listitem><para>Set variables, parse blocks</para></listitem> <listitem><para>Finish and output</para></listitem> </orderedlist> </para> <para> <emphasis>Variables</emphasis> are placeholders in your HTML code that can be replaced with dynamic values provided from database or calculated in your code. An example for a variable is <literal>{CODE_AUTHOR}</literal>: You enclose the variable name in curly braces. The name of the variable may contain any characters, except spaces, tabs and newlines. </para> <para> <emphasis>Blocks</emphasis> surround certain pieces of HTML code and can be re-used, e.g. a <literal><tr></literal>row in a table. Blocks are defined by using HTML comments, containing <literal>BEGIN</literal> or <literal>END</literal>, and the block name. 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>Simple example</title> <example> <title>Template file: <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>PHP code: <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>Loading files</title> <para> Template file locations are relative to a definable <emphasis>root</emphasis> directory. 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>Defining blocks</title> <para> If you want to re-use certains parts of your HTML code multiple times, e.g. rows in a table, you can define blocks as described in the example above. </para> <para> Blocks are not automatically detected, they need to be defined explicitely via the <function>setBlock</function> method. 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>Finish & output</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>Finishing a file and writing it out</title> <programlisting role="php"><![CDATA[ $t->parse('OUT', 'authors'); $htmlcode = $t->finish( $tpl->get('OUT') ); echo $htmlcode; ]]></programlisting> </example> <example> <title>Finishing a file and writing it out, the easy way</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 -->