Article
Beyond The Template Engine
Setting Multiple Variables
How can we set multiple variables sinultaneously? Here is an example that uses a method contributed by Ricardo Garcia.
<?php
require_once('template.php');
$tpl = & new Template('./templates/');
$tpl->set('title', 'User Profile');
$profile = array(
'name' => 'Frank',
'email' => 'frank@bob.com',
'password' => 'ultra_secret'
);
$tpl->set_vars($profile);
echo $tpl->fetch('profile.tpl.php');
?>
The associated template looks like this:
<table cellpadding="3" border="0" cellspacing="1">
<tr>
<td>Name</td>
<td><?=$name;?></td>
</tr>
<tr>
<td>Email</td>
<td><?=$email;?></td>
</tr>
<tr>
<td>Password</td>
<td><?=$password;?></td>
</tr>
</table>
And the parsed output is as follows:
<table cellpadding="3" border="0" cellspacing="1">
<tr>
<td>Name</td>
<td>Frank</td>
</tr>
<tr>
<td>Email</td>
<td>frank@bob.com</td>
</tr>
<tr>
<td>Password</td>
<td>ultra_secret</td>
</tr>
</table>
Special thanks to Ricardo Garcia and to Harry Fuecks for thei contributions to this article.
Related Links
Here's a list of good resources for exploring template engines in general.
- Web Application Toolkit Template View - a wealth of information about all types of template approaches
- MVC Pattern - description of 3-tier application design
- SimpleT - another php-based template engine that uses PEAR::Cache_Lite
- Templates and Template Engines - more on various template implementations
- Smarty - compiling template engine