Main Page | Modules | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

Template-Example

Sections:

The TPL language

{variable_name}Including a normal variable value.
{array_variable_name.0}Including a single array value.
<include filename="include_file.tpl">Including an additional template file.
<loop>{array_variable_name}</loop>Inlcuding an complete data array.

Example code PHP preparing

The following code demonstrates how to use the class.

<?php
/*!***********************************************************************
 *************************************************************************
 * \file            template-sample.php
 *
 * \author          Kai Klenovsek
 *
 * \date            First Step: 2004-12-22 <br>
 *
 * \note <br>
 * \b THE \bBEER-WARE \bLICENSE <br>
 *  As long as you retain this notice you can do whatever you want with <br>
 *  this stuff. If we meet some day, and you think this stuff is worth it, <br>
 *  you can buy me a beer in return. <br>
 *
 * \b NOTES: <br>
 *  
***************************************************************************
***************************************************************************/

// First we have to inlcude the phplibex
require ("../../phplibex.inc.php");

/* Generate class object with an absolut path to the template folder
 * This sample is optimized for XAMPP */
$tpl = new templateparser("C:/apachefriends/xampp/htdocs/phplibex/samples/template/");

// Generate variable
$testmessage = "Hello World";
   
/* Now we register the variable and set the name for the request
 * in the TPL file. */
$tpl->tpl_register_var( $testmessage, "testmessage" );

// Register some vars direct by reference
$tpl->tpl_register_var( "I`m a test header", "header" );
$tpl->tpl_register_var( "Test-Title", "titel" );
$tpl->tpl_register_var( "V1.00", "version" );
$tpl->tpl_register_var( "Template", "surename" );
$tpl->tpl_register_var( "Parser", "lastname" );

// Generate and register an array
$myarray1[] = "Array 1 Field 0";
$myarray1[] = "Array 1 Field 1";
$myarray1[] = "Array 1 Field 2";
$myarray1[] = "Array 1 Field 3";
$myarray1[] = "Array 1 Field 4";
$myarray1[] = "Array 1 Field 5";
$myarray1[] = "Array 1 Field 6";
$myarray1[] = "Array 1 Field 7";
$tpl->tpl_register_var( $myarray1, "testarray1" );

// Generate and register another array
$myarray2[] = "Array 2 Field 0";
$myarray2[] = "Array 2 Field 1";
$myarray2[] = "Array 2 Field 2";
$myarray2[] = "Array 2 Field 3";
$myarray2[] = "Array 2 Field 4";
$myarray2[] = "Array 2 Field 5";
$myarray2[] = "Array 2 Field 6";
$myarray2[] = "Array 2 Field 7";
$tpl->tpl_register_var( $myarray2, "testarray2" );
    
// Parse test template
$tpl->tpl_parse_all("test.tpl");
    
// Show the test template
$tpl->tpl_display_tpl( );
    
// Get the error list and show it. Normaly it should be empty :)
$errors = $tpl->tpl_get_errorlist( );
    
for ( $loop=0; $loop <= sizeof($errors); $loop++ )
    echo "<b>".$errors[$loop]."</b><br>";    
    
// Its is also possible to delete registered arrays or vars.
$tpl->tpl_unset_array( "testarray1" );
$tpl->tpl_unset_array( "testarray2" );

?>

Example code TPL scripting

<html>
 <head>
  <META NAME="Author"         CONTENT="Kai Klenovsek">
  <META NAME="Date"           CONTENT="22.12.2004">
  <META NAME="Subject"        CONTENT="Template-Test">
 
  <title> {titel} </title>
 </head>
    
 <body>

 <center><h2> {header} </h2></center>

 <p>
 Version: <b>{version}</b><br>
 Surename: <b>{surename}</b><br>
 Lastname: <b>{lastname}</b> <br>
 Testmessage: <b>{testmessage}</b><br>

 <include filename="include_file.tpl">
 <br>
 Single array value: <b>{testarray1.0}</b>

 <br><br><br>

 <table border="1">
  <loop><tr><td>{testarray2}</td><td>{testarray1}</td></tr></loop>
 </table>

 </p>

 </body>
</html>

Related links:

What is a template: http://en.wikipedia.org/wiki/Template
Generated on Fri Aug 26 08:13:54 2005 for PHP-Lib-Xtreme by  doxygen 1.4.3