XiTemplate Home Page XiTemplate
[ class tree: XiTemplate ] [ index: XiTemplate ] [ all elements ]
Prev Next
parsing blocks

parsing blocks

using parse() to build your document

Parsing

By now you are at least somewhat familiar with the parse() method. In the following examples the parse method will be explained in more detail.

The parse() Method

The parse() method, simply put, is used to parse blocks within your current template object. Earlier we discussed block syntax and block hierarchy. We know all about the main block and sub blocks. Going forward, we'll concentrate on complex block structures and controlling those structures with parse().

Consider the following 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/example.tpl');
  7.  
  8. /*let's make dynamically make a table*/
  9. $rows = 3;
  10. $cols = 4;
  11.  
  12. for ($i = 0; $i < $rows; $i++) /*loop to make the tr's*/
  13. {
  14. for ($x = 0; $x < $cols; $x++) /*loop to make the td's*/
  15. {
  16. $xitpl->assign('VARIABLE', 'row: '.($i+1).', column: '.($x+1));
  17. /*notice the hierarchy by which blocks must be parsed...*/
  18. $xitpl->parse('main.tr.td');
  19. }
  20. /*...always parse child blocks first*/
  21. $xitpl->parse('main.tr');
  22. }
  23.  
  24. /*parse the "main" block of the template (the whole page)*/
  25. $xitpl->parse('main');
  26.  
  27. /*send the final page out to the browser*/
  28. $xitpl->out('main');
  29. ?>

Here is a sample template to work with the above PHP script:

<!-- BEGIN: main -->

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

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

<body>

<h1>XiTemplate</h1>

<p>Here is a table made up of several sub blocks</p>

<table border="1" cellpadding="2" cellspacing="0">
	<!-- BEGIN: tr -->
	<tr>
		<!-- BEGIN: td -->
		<td>{VARIABLE}</td>
		<!-- END: td -->
	</tr>
	<!-- END: tr -->
</table>

</body>
</html>

<!-- END: main -->

In the above example there are three blocks: 1) the main block, which constitutes the entire page, 2) a block named "tr", and 3) a block named "td".

There are a few important things to take note of when dealing with multiple blocks and nested blocks.

  • Nested child blocks must always be parsed before the parent block they live in.

  • Child blocks must be terminated with an END tag before the parent block is terminated. Similar to HTML, you would never close a table tag before closing a table row.

  • Template variables (including $nullValue) must be assigned prior to parsing the target block where the template variable lives.

  • Blocks can be parsed over and over again as many times as you need them to be. In the above example both "tr" and "td" are parsed multiple times to create a complex table.

That about covers parse() in all its wonder.

Next in this tutorial is rparse(). Click "next" to learn more.


Prev Up Next
Null Values XiTemplate Manual Recursive Parse

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