DotKernel PSR-7 Middleware Applications

Dot_Template – Introductory Tutorial

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);


 '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']);
		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);
		// 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
// 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 ...



This is an example on how to use Dot_Template class:

Fruit Name Fruit Color Fruit Amount

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>