In the HMVC structure of DotKernel, the View uses a Template Engine. The DotKernel Template Engine is an implementation of the PHPLib Template engine for PHP5 – HTML_Template_PHPLIB.  We use it because it has an amazing ability to separate the application code from the presentation layer. It separates the manipulation of data (in the database, as well as in the PHP code) from its final format, whether that format is HTML, XML or a formatted e-mail. The great advantage is that it allows us to change the look and feel of a site quickly without having to dig around into a lot of PHP variable assignments and print statements.

The Dot_Template class is inspired from HTML_Template_PHPLIB and improved for DotKernel requirements.

Below are some examples on how to use the major methods from Dot_Template:

Note*: Read the phpDocumentation for each method from Dot_Template class to better understand their usage.

  • setFile – sets the appropriate template file that will be outputted to the browser
    setFile($varname, $filename = ”);
  • setVar – sets the value of a variable
    setVar($varname, $value = ”, $append = false);
  • setBlock – sets a block in the appropriate template handle
    setBlock($parent, $varname, string $name = ”);
    A block is a segment of code that can repeat itself.
  • parse – parses the handle into the target
    parse($target, $varname, $append = false);

index.php

 'Apple', 'color'=> array('red','green','gold')),
		array('name' => 'Orange', 'color'=> 'orange'),
		array('name' => 'Strawberry', 'color'=> 'red'),
		array('name' => 'Lemon', 'color'=> array('yellow','green')),
		array('name' => 'Grapes', 'color'=> array('black','white')),
		array('name' => 'Peach', 'color'=> 'orange')
);
$amount = array('2.00', '3.52', '4.50', '3.75', '4.23', '4.15');
//set the file that will be parse
$tpl->setFile('tpl_index', 'index.tpl');
// The block row2 must be declare before block row1
// because in the TPL file row2 is inside row1
 
// initialization of the block
$tpl->setBlock('tpl_index', 'row2', 'block_row2');
$tpl->setBlock('tpl_index', 'row1', 'block_row1');
 
foreach($fruit as $key => $val)
{
	// initialize of the block row2 - actualy, clearing the content of block2
	// comment the below line to see what's happening
	$tpl->parse('block_row2', '');
	// the content of {FRUIT_NAME} from the TPL file, will be replaced with the value of    		   $val['name']
	$tpl->setVar('FRUIT_NAME', $val['name']);
	if(is_array($val['color']))
	{
		foreach($val['color'] as $color)
		{
			$tpl->setVar('FRUIT_COLOR', $color);
			// parse the content of the block, usually on the end of the loop 					   instruction
			$tpl->parse('block_row2', 'row2', true);
		}
	}
	else
	{
		$tpl->setVar('FRUIT_COLOR',$val['color']);
		// parse the content of the block
		$tpl->parse('block_row2', 'row2', true);
	}
	$tpl->setVar('FRUIT_AMOUNT', $amount[$key]);
	// parse the content of the block, usually on the end of the loop instruction
	$tpl->parse('block_row1', 'row1', true);
}
// output the content of the TPL file.
// even if {OUTPUT} variable doesn't exists, pparse echos tpl_index content
$tpl->pparse('OUTPUT','tpl_index');
/*
// in this case, if {OUTPUT} would be a variable in a TPL file, the content of tpl_index is parsed into it
// so you could have TPL file1 parsed inside TPL file2 parsed inside TPL file3 and so on ...
$tpl->parse('OUTPUT','tpl_index');
*/

index.tpl

 
 <!--
	table
	{
		border: 1px solid #667EA8;
		background-color: #E4E4E4;
	}
	td
	{
		background-color: #FFFFFF;
	}
-->
<h3>This is an example on how to use <em>Dot_Template</em> class:</h3>
<table cellspacing="1" cellpadding="0" width="100%">
<tbody>
<tr>
<td>Fruit Name</td>
<td>Fruit Color</td>
<td>Fruit Amount</td>
</tr>
<!-- BEGIN row1 -->
<tr>
<td>{FRUIT_NAME}</td>
<td style="background-color: #ffe8e8;">
				<!-- BEGIN row2 -->
				<span style="color: #0f0000;">{FRUIT_COLOR}</span>
				<!-- END row2 --></td>
<td>{FRUIT_AMOUNT}</td>
</tr>
<!-- END row1 --></tbody>
</table>

Place all these 3 files (Dot_Template.php, index.php and index.tpl) in the same location to see how they work together.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>