HTML_Template_IT::setVariable()

HTML_Template_IT::setVariable() – set a variable

Synopsis

require_once 'HTML/Template/IT.php';

void HTML_Template_IT::setVariable ( mixed $placeholder , mixed $variable = "" )

Description

Set the value of a variable in the current template block. If $placeholder is an array, the key of an element is treated as a placeholder name while the value is treated as its substitution.

Parameter

  • mixed $placeholder - name of the placeholder to substitute or a array with the placeholder as key and the data to assign as value.

  • mixed $variable - if $placeholder is not a array, the value to assign to the placeholder.

Example

Template - cvsnames.tpl.htm

<html>
 <table>
<!-- BEGIN row -->
  <tr>
   <td>
    {CVS_USERNAME}
   </td>
   <td>
    {REALNAME}
   </td>
   <td>
    <ul>
    <!-- BEGIN project_row -->
     <li>{PROJECT}</li>
    <!-- END project_row -->
    </ul>
   </td>
  </tr>
<!-- END row -->
 </table>
</html>

Script

<?php
  
require_once "HTML/Template/IT.php";

  
$data = array
  (
    
"0" => array("cvs_username" => "pajoye"
                 
"realname" => "Pierre-Alain Joye",
                 
"projects" => array("PEAR"
                                     
"PEAR_Frontend_Web"
                                     
"PEAR_RemoteInstaller"
                                      
"HTML_Template_IT")),
    
"1" => array("cvs_username" => "dsp",
                 
"realname" => "David Soria Parra",
                 
"projects" => array("HTML_Template_IT"))
  );

  
$tpl = new HTML_Template_IT("./templates");

  
$tpl->loadTemplatefile("cvsnames.tpl.htm"truetrue);

  foreach(
$data as $name) {
     
// Assign data to the inner block
     
     
$tpl->setCurrentBlock("project_row");
     foreach (
$name['projects'] as $projectname) {
         
$tpl->setVariable("PROJECT"$projectname);
         
$tpl->parseCurrentBlock();
     }
     
     
// use the possbility to set the placeholders using an assoc array
     
$tpl->setVariable(
                      array(
"CVS_USERNAME" => $name["cvs_username"],
                            
"REALNAME" => $name["realname"])
                      );

     
$tpl->parse("row");
  }

  
// show() parses the __global__ block and
  // print the output
  
$tpl->show();

?>

Note

This function can not be called statically.

set a template (Previous) print a block including replacments (Next)
Last updated: Sun, 19 Oct 2014 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report or add a note.
View this page in:

User Notes:

There are no user contributed notes for this page.