PHP XTemplate: Multiple Dynamic Blocks

In PHP XTemplate: The Basics, I covered the basics for using the XTemplate engine. In this tutorial, I will cover using dynamic blocks to fill in information from a data source. For this tutorial, I will use an XML file for my data source. For more information on using PHP to read XML files, see my tutorials on Reading XML files with PHP. I will use the XML file created in the first part of that series.

The Template

<!-- BEGIN: main -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
 <head>
 <title>Documents Home Page</title>
 </head>
 <body>
 <h1>Documentation</h1>
 <!-- BEGIN: category -->
 <h2>{CATEGORY}</h2>
 <ul>
 <!-- BEGIN: item -->
 <li><a href="{DOC.LINK}" target="_blank">{DOC.NAME}</a></li>
 <!-- END: item -->
 </ul>
 <!-- END: category -->
 </body>
</html>
<!-- END: main -->

You will notice that I wrapped the entire document in a block named main. Inside the document, I have two subblocks: category and item. Also, note that subblock item is inside the category subblock. There are three tags:

{CATEGORY} – the title for each category
{DOC.LINK} – the URL to the documentation
{DOC.NAME} – the name of the documentation being linked to

You may notice the dot notation in the last two tags. They will receive information from an associative array we will pass to the engine. More on that in a minute.

The PHP Code

<?php
include_once('./XTemplate.class.php');
// get the main template
$xtpl = new XTemplate('./main.tpl');

// process and assign documentation xml file
// Process documentation list from webdoc.xml
$docs = simplexml_load_file("webdoc.xml");

foreach ($docs->category as $category) {
 $xtpl->assign('CATEGORY', $category['name']);
 // links for each category
 foreach ($category->doc as $document) {
 $doc['LINK'] = $document->link;
 $doc['NAME'] = $document->name;
 $xtpl->insert_loop('main.category.item', array('DOC'=>$doc));
 }
 $xtpl->parse('main.category');
}

// output to screen
$xtpl->parse('main');
$xtpl->out('main');
?>

In lines 1-4, I include the XTemplate engine and load the template, sample2.tpl. In line 8, I use simplexml_load_file to create a simplexml object.

The heart of the work in done in the foreach loops. The outside loop processes each of the categories defined in the XML file. The inside loop processes each of the links defined for the category.

In line 11, I assign the name of the category to the {CATEGORY} tag.

In lines 14-15, I create an associative array using the the link and name elements for each document. Note that the keys for the array (LINK and NAME) correlate to the dot notation in the templates item block. The case of the keys matter. If they are lower case in the template, they need to be lower case in the PHP assignment. The same in true for upper case. I use upper case as a matter of preference. I find it easier to locate the tags in the template when they are all in upper case.

In line 16, I call the XTemplate’s insert_loop() function to write the item block information into memory. The insert_loop() function takes two arguments: the name of the block to parse and an array containing the tag name and the data to assign. The block is addressed using dot notation. Since the item block resides in the category block and the category block resides in the main block, I reference it with main.category.item. In the array, I reference the tag DOC and pass it the associative array we created. This is the reason for the dot notation in the template. The keys in the array will be assigned to the dot tags in the template:

$doc[‘LINK’] = {DOC.LINK}
$doc[‘NAME’] = {DOC.NAME}

In line 18, I parse the category block into memory. Once again, notice the dot notation used with the block name (main.category).

The two foreach loops are repeated for each of the categories and documents in our XML file.

Finally, in lines 22-23, I parse the main block and output the generated XHTML file to the browser.

Next time, we will import other template files into our main template and extend the usability of the engine even further.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s