XiTemplate Home Page XiTemplate
[ class tree: XiTemplate ] [ index: XiTemplate ] [ all elements ]
Prev Next
Null Values

Null Values

Custom handling of null values

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

  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/nullValue.tpl');
  6. /*multidimensional array*/
  7. $myArray = array
  8. (
  9. 'name' => 'Jim',
  10. 'lastName' => 'Grill',
  11. 'address' => array
  12. (
  13. 'street' => 'One XiTemplate Way',
  14. 'city' => 'Paradise',
  15. 'state' => '', /* No state specified. This is a null value.*/
  16. 'zip' => '12345'
  17. ),
  18. 'age' => 'too old'
  19. );
  20. /*assign the array to a template variable*/
  21. $xitpl->assign('MYARRAY', $myArray);
  22. /*We don't want to display null values so we assign 'Unknown' instead*/
  23. /*This can be done at any time as long it's before the current block is parsed*/
  24. $xitpl->nullValue = 'Unknown';
  25. /*parse the first block*/
  26. $xitpl->parse('main.firstperson');
  27. /*null values will be ok from this point so we reset it to ''.*/
  28. /*This can be done at anytime before the next block is parsed so we*/
  29. /*could have done it on line 60 if we wanted to.*/
  30. $xitpl->nullValue = '';
  31. /*multidimensional array*/
  32. $myArray = array
  33. (
  34. 'name' => 'Travis',
  35. 'lastName' => 'Miller',
  36. 'address' => array
  37. (
  38. 'street' => 'One Coder\'s Way',
  39. 'city' => 'Linux Valley',
  40. /* No state specified. This is a null value.*/
  41. 'state' => '',
  42. 'zip' => '54321'
  43. ),
  44. 'age' => 'too young'
  45. );
  46. /*assign the array to a template variable*/
  47. $xitpl->assign('MYARRAY', $myArray);
  48.  
  49. /*parse the second block*/
  50. $xitpl->parse('main.secondperson');
  51.  
  52. /*parse the "main" block of the page (the whole page)*/
  53. $xitpl->parse('main');
  54.  
  55. /*send the final page out to the browser*/
  56. $xitpl->out('main');
  57. ?>

...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 "&nbsp;" 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!

Prev Up Next
Assign() XiTemplate Manual parsing blocks

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