Smarty php Crash Course phpma
For those of you who have used PHP template engines, the basic concepts of Smarty should look quite familiar. In your PHP application you assign variables for use in the template, then you display it.
| index.php |
|---|
include('Smarty.class.php');
// create object
$smarty = new Smarty;
// assign some content. This would typically come from
// a database or other source, but we'll use static
// values for the purpose of this example.
$smarty->assign('name', 'george smith');
$smarty->assign('address', '45th & Harris');
// display it
$smarty->display('index.tpl');
|
The template file then contains the output interspersed with tags that Smarty replaces with assigned content.
| index.tpl | output | |
|---|---|---|
<html>
<head>
<title>User Info</title>
</head>
<body>
User Information:<p>
Name: {$name}<br>
Address: {$address}<br>
</body>
</html>
|
<html> <head> <title>User Info</title> </head> <body> User Information:<p> Name: george smith<br> Address: 45th & Harris<br> </body> </html> |
As you can see, Smarty cleanly separates your presentation elements (HTML, CSS, etc.) from your application code. However, it does not necessarily separate logic entirely from your templates. With respect to Smarty's design principles, so long as the logic in the templates is for presentation only, it is permissible to use it. For instance, looping over table row colors or including one template from another would be considered presentation logic. This is something the application should not need to accomodate (it just supplies an array of data.) The idea is to keep the template designer role and application programming role separated. You should be able to completely tear down the templates and rebuild them without touching the code base, all while retaining full control of the presentation. With this comes an important point: there is nothing stopping you from putting application logic in the template. Smarty has more than enough power to let you shoot yourself in the foot, so you must have some self discipline in this respect. Don't worry though, with a little bit of practice it becomes quite self-evident what logic belongs where. phpma
Smarty has some nice features that take advantage of this design principle, as you'll see in the next example. One prominant feature of Smarty is variable modifiers. These are used to alter the output of assigned variables from within the template. In our example, we would like to display George's name capitalized, and we would like to properly HTML escape the amphersand (&) symbol in the address. We also show how to display the current date with a custom formatting. phpma
| index.tpl | output | |
|---|---|---|
<html>
<head>
<title>User Info</title>
</head>
<body>
User Information:<p>
Name: {$name|capitalize}<br>
Addr: {$address|escape}<br>
Date: {$smarty.now|date_format:"%Y-%m-%d"}<br>
</body>
</html>
|
<html> <head> <title>User Info</title> </head> <body> User Information:<p> Name: George Smith<br> Addr: 45th & Harris<br> Date: 2003-12-19<br> </body> </html> |
Using modifiers, we have just handed formatting control of the assigned content over to the template and out of the application logic. Now we can assign the raw data straight to the template (no mods necessary for presentation purposes!) You can chain any number of modifiers together on one variable, making this feature quite flexible. There are many more modifiersthat come with Smarty, or you can make your own with its easy to use plugin architecture. Drop your new modifier into the plugin directory, then mention it in the template!


