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
- <?php
- /*include the class*/
- include_once('../class.XiTemplate.php');
-
- /*instantiate the class and pass the path to your HTML template file*/
- $xitpl = new XiTemplate('templates/example.tpl');
-
- /*assign a value to a template variable.*\
- /*In this example {NAME} will be replaced with "XiTemplate"*/
- $xitpl->assign('NAME', 'XiTemplate');
-
- /*parse the "main" block of the page (the whole page)*/
- $xitpl->parse('main');
-
- /*send the final page out to the browser*/
- $xitpl->out('main');
- ?>
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
- <?php
- /*include the class*/
- include_once('../class.XiTemplate.php');
-
- /*instantiate the class and pass the path to your HTML template file*/
- $xitpl = new XiTemplate('templates/assignTutorial.tpl');
-
- /*numeric indexed single dimensional array*/
- $array = array();
- $array[] = 'apple';
- $array[] = 'orange';
-
- /*assign the array*/
- $xitpl->assign('ARRAY', $array);
-
- /*numeric indexed multidimensional array*/
- $array2 = array();
- $array2[] = 'apple';
- $array2[] = 'orange';
- $array2[] = array('banana','grape');
-
- /*assign the array*/
- $xitpl->assign('ARRAY2', $array2);
-
-
- /*associative multidimensional array*/
- $myArray = array
- (
- 'name' => 'Jim',
- 'lastName' => 'Grill',
- 'address' => array
- (
- 'street' => 'One XiTemplate Way',
- 'city' => 'Paradise',
- 'state' => 'Hawaii',
- 'zip' => '12345'
- ),
- 'age' => 'too old'
- );
-
- /*assign the array to a template variable*/
- $xitpl->assign('MYARRAY', $myArray);
-
- /*parse the "main" block of the page (the whole page)*/
- $xitpl->parse('main');
-
- /*send the final page out to the browser*/
- $xitpl->out('main');
- ?>
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.