XiTemplate Home Page XiTemplate
[ class tree: XiTemplate ] [ index: XiTemplate ] [ all elements ]
Prev Next
Assign()

Assign()

Assigning values to template variables

The assign() method

You've made it this far... great!

Now I'll cover the most (ab)used method of XiTemplate: the assign() method.

From our previous example we discovered that when the template variable {NAME} was not assigned in our PHP script it ceased to exist in the parsed page. This is a feature of XiTemplate. There will be times when you do not need variable to be assigned and you will not want it to show up in the final output web page.

You may have also noticed that if you view the source of your web page that all the block markers are missing as well. That is the beauty of XiTemplate; it operates invisibly and seamlessly to the viewer of the final product.

It's important to point out that normal HTML comments beginning with <!-- are not recognized by XiTemplate and thus stay in your template through the parsing stage.

Let's move on to assigning template variables.

Scalar Variables

A scalar variable is single value, be it a number or a string. Assigning a scalar value to a template variable is pretty straight forward in XiTemplate. Using the same template as in the previous example, copy and paste the following code into your PHP script.

Example

  1. <?php
  2. /*include the class*/
  3. include_once('../class.XiTemplate.php');
  4. /*instantiate the class and pass the path to your HTML template file*/
  5. $xitpl = new XiTemplate('templates/example.tpl');
  6.  
  7. /*assign a value to a template variable.*\
  8. /*In this example {NAME} will be replaced with "XiTemplate"*/
  9. $xitpl->assign('NAME', 'XiTemplate');
  10.  
  11. /*parse the "main" block of the page (the whole page)*/
  12. $xitpl->parse('main');
  13. /*send the final page out to the browser*/
  14. $xitpl->out('main');
  15. ?>

Array Variables

XiTemplate also supports non-scalar array-type variables.

Interpolation of both numeric and associative indexed single and multidimensional arrays is supported.

In the following example I'll cover all array types.

Example

  1. <?php
  2. /*include the class*/
  3. include_once('../class.XiTemplate.php');
  4.  
  5. /*instantiate the class and pass the path to your HTML template file*/
  6. $xitpl = new XiTemplate('templates/assignTutorial.tpl');
  7.  
  8. /*numeric indexed single dimensional array*/
  9. $array = array();
  10. $array[] = 'apple';
  11. $array[] = 'orange';
  12.  
  13. /*assign the array*/
  14. $xitpl->assign('ARRAY', $array);
  15.  
  16. /*numeric indexed multidimensional array*/
  17. $array2 = array();
  18. $array2[] = 'apple';
  19. $array2[] = 'orange';
  20. $array2[] = array('banana','grape');
  21.  
  22. /*assign the array*/
  23. $xitpl->assign('ARRAY2', $array2);
  24.  
  25.  
  26. /*associative multidimensional array*/
  27. $myArray = array
  28. (
  29. 'name' => 'Jim',
  30. 'lastName' => 'Grill',
  31. 'address' => array
  32. (
  33. 'street' => 'One XiTemplate Way',
  34. 'city' => 'Paradise',
  35. 'state' => 'Hawaii',
  36. 'zip' => '12345'
  37. ),
  38. 'age' => 'too old'
  39. );
  40.  
  41. /*assign the array to a template variable*/
  42. $xitpl->assign('MYARRAY', $myArray);
  43.  
  44. /*parse the "main" block of the page (the whole page)*/
  45. $xitpl->parse('main');
  46. /*send the final page out to the browser*/
  47. $xitpl->out('main');
  48. ?>

Here is the template used in this example. Copy it and name it assignTutorial.tpl.

<!-- BEGIN: main -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>XiTemplate</title>
</head>

<body>

<h1>XiTemplate</h1>

<p>Sometimes I like to eat an {ARRAY.0}.
I also enjoy the juice from an {ARRAY.1}.</p>

<p>Although I like {ARRAY2.0}s and 
{ARRAY2.1}s, I do not care for 
{ARRAY2.2.0}s and {ARRAY2.2.1}s.</p>

<p>This example was written by:</p>

<p>{MYARRAY.name} {MYARRAY.lastName}<br>
{MYARRAY.address.street}<br>
{MYARRAY.address.city}, {MYARRAY.address.state} 
{MYARRAY.address.zip}
</p>

</body>
</html>

<!-- END: main -->

Spend some time playing with this example to get familiar with scalar and array variables.

Please continue to learn about custom handling of null values.


Prev Up Next
XiTemplate Logic XiTemplate Manual Null Values

Documentation generated on Fri, 20 Aug 2004 10:57:41 -0500 by phpDocumentor 1.3.0RC3