Article

Beyond The Template Engine

Page: 1 2 3 4 5 Next

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.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links