Null Values
As you know from the previous examples, XiTemplate automatically
replaces unassigned template variables with a null value.
Sometimes it may become necessary to handle unassigned template
variables in a more specific way. One example that comes to mind is when
using XiTemplate to populate cells in an HTML table. Empty table cells can
create problems for some browsers particularly when the cell contains a
background element or other properties that rely on the cell being
populated.
XiTemplate provides a class member variable to solve this problem.
This variable is called $nullValue and can be accessed in the same way a
method is accessed.
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/nullValue.tpl');
-
- /*multidimensional array*/
- $myArray = array
- (
- 'name' => 'Jim',
- 'lastName' => 'Grill',
- 'address' => array
- (
- 'street' => 'One XiTemplate Way',
- 'city' => 'Paradise',
- 'state' => '', /* No state specified. This is a null value.*/
- 'zip' => '12345'
- ),
- 'age' => 'too old'
- );
-
- /*assign the array to a template variable*/
- $xitpl->assign('MYARRAY', $myArray);
-
- /*We don't want to display null values so we assign 'Unknown' instead*/
- /*This can be done at any time as long it's before the current block is parsed*/
- $xitpl->nullValue = 'Unknown';
-
- /*parse the first block*/
- $xitpl->parse('main.firstperson');
-
- /*null values will be ok from this point so we reset it to ''.*/
- /*This can be done at anytime before the next block is parsed so we*/
- /*could have done it on line 60 if we wanted to.*/
- $xitpl->nullValue = '';
-
- /*multidimensional array*/
- $myArray = array
- (
- 'name' => 'Travis',
- 'lastName' => 'Miller',
- 'address' => array
- (
- 'street' => 'One Coder\'s Way',
- 'city' => 'Linux Valley',
- /* No state specified. This is a null value.*/
- 'state' => '',
- 'zip' => '54321'
- ),
- 'age' => 'too young'
- );
-
- /*assign the array to a template variable*/
- $xitpl->assign('MYARRAY', $myArray);
-
- /*parse the second block*/
- $xitpl->parse('main.secondperson');
-
- /*parse the "main" block of the page (the whole page)*/
- $xitpl->parse('main');
-
- /*send the final page out to the browser*/
- $xitpl->out('main');
- ?>
...and here is the HTML template
<!-- BEGIN: main -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>XiTemplate</title>
</head>
<body>
<h1>XiTemplate</h1>
<!-- BEGIN: firstperson -->
<p>This example was written by:</p>
<p>{MYARRAY.name} {MYARRAY.lastName}<br>
<p>Age: {MYARRAY.age}</p>
<p>Address:</p>
{MYARRAY.address.street}<br>
{MYARRAY.address.city}, {MYARRAY.address.state} {MYARRAY.address.zip}
</p>
<!-- END: firstperson -->
<!-- BEGIN: secondperson -->
<p>{MYARRAY.name} {MYARRAY.lastName} contributed some very
important changes to {NAME}.</p>
<p>Age: {MYARRAY.age}</p>
<p>Address:</p>
{MYARRAY.address.street}<br>
{MYARRAY.address.city}, {MYARRAY.address.state} {MYARRAY.address.zip}
</p>
<!-- END: secondperson -->
</body>
</html>
<!-- END: main -->
When you get this example working you will notice that the first
block is parsed with null values set to "Unknown". On line 40 $nullValue
is reset back to the default, which is '' - or - nothing.
The reason for this behavior is that you may want to replace
unassigned variables with a default string like " " temporarily
like while parsing a table. Since XiTemplate has no idea how much you need
parse, the value you assigned to $nullValue will remain as you set it
until told otherwise.
The $nullValue variable can be set at any time as long as it is
before the target block gets parsed. This behavior is true of all template
variables.
Stay tuned for parsing. It gets very exciting from here. I
promise!