10. Templating

In Eden, we promote PHP templating and provided a one stop class to simply bind variables to template files or strings and return the final results.

Figure 1. Template String
echo eden('template')
	->set('[name]', 'Chris')
	->set('[job]', 'author')
	->parseString('I am [name] an [job]'); //--> I am Chris an author

The above explains a simple example to create a string template. the set() method sets variable names to values, which later on will attempt to replace these variables found in the template with the actual value. parseString() will consider the given string and return the final string output. Although this is a rudimentary example, Figure 2 explains how we could bind template variables to a PHP file containing PHP and HTML.

Figure 2. PHP Templating
echo eden('template')
	->set('name', 'Chris')
	->set('job', array('author', 'programmer'))
	->parsePHP(dirname(__FILE__).'/template.php'); //--> I am Chris an author, programmer
Figure 3. Sample PHP Template File
<p>I am <?php echo $name; ?> an <?php echo implode(', ', $job); ?></p>

Figure 2 and Figure 3 explains how to configure a PHP template. parsePHP() will load the given file and return the final string output. It's of note to know that variable names should follow PHP variable naming conventions to prevent Fatal Errors. You can also bind variables by groups using set() as in Figure 4

Figure 4. Binding a group of variables
echo eden('template')
		'name' => 'Chris',
		'job' => array('author', 'programmer'))
	->parsePHP(dirname(__FILE__).'/template.php'); //--> I am Chris an author, programmer

It's also common practice to wrap the template class into a function or method for an even simplier interface.

Figure 4. Template function
function template($file, array $data = array()) {
	return Eden_Template::i()->set($data)->parsePhp($file);

Template System

Because Eden's template object is simple, doesn't mean that it's not robust. In fact we use this simple concept to combine reusable templates together into a single output. Figure 5 shows an example of how to define partial templates combining into one master template.

Figure 5. Templating With Partials
$root = dirname(__FILE__);
$head = template($root.'/head.phtml', array('active' => 'home'));
$body = template($root.'/body.phtml', array('name' => 'Chris'));
$foot = template($root.'/foot.phtml');

return template($root.'/page.phtml', array(
	'title'			=> 'Welcome Page',
	'class'			=> 'home',
	'head'			=> $head,
	'body'			=> $body,
	'foot'			=> $foot));
Though we are using .phtml in this example, it's important to know you can use any extension you like.
Figure 5a. head.phtml
	<li class="<?php echo $active=='home' ? 'active':NULL; ?>">Home</li>
	<li class="<?php echo $active=='posts' ? 'active':NULL; ?>">Posts</li>
Figure 5b. body.phtml
<h1>Welcome <?php echo $name; ?></h1>
Figure 5c. foot.phtml
&copy;Openovate Labs
Figure 5a. page.phtml
<!DOCTYPE html>
<html class="<?php echo $class; ?>">
	<title><?php echo $title; ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<div class="page">
		<div class="head"><?php echo $head; ?></div>
		<div class="body"><?php echo $body; ?></div>
		<div class="foot"><?php echo $foot; ?></div>

Templating in Eden is pretty easy to understand returning output with the lowest overhead. We stayed away from trying to make Eden's templating into a suite like Twig or Smarty because it limits the end developer's choices on template solutions. Eden's templating is a utility class, and Eden has several utility classes defined against common programatic routines a developer usually undergoes. The next section breifly covers another utility class covering common country methods. Head over to 11. Countries whenever your ready.

© 2012 Openovate Labs. All rights reserved.