Smarty has a unique feature called variable modifiers that can alter the content 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.
Name: George Smith<br> Address: 45th & Harris<br>
</body> </html>
Pretty easy! You can also chain modifiers together on one variable, making this feature quite flexible.
There are many more href="http://smarty.php.net/manual/en/language.modifiers.php">modifiers that 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!
干得漂亮!(ft...)你同样可以将调节器链接到一个变量上面,让它更灵活.
Smarty自带了很多调节器,或者你可以使用插件机制diy一个.
把你自己做的调节器放到插件目录里,就会在模板里起作用!
Smarty also has template functions that can carry out tasks.
For example, you can include other templates from within a template with the include function.
Let's say you have many templates with the same header and footer information. You can manage these as separate templates and include them.
<html>
<head>
<title>
User Info</title>
</head>
<body>
User Information:<p>
Name: George Smith<br> Address: 45th & Harris<br>
</body>
</html>
Notice that $title is a template variable not assigned in the PHP code, but assigned by passing it as an attribute to the include function.
This way the $title variable is only available within the scope of the header template, and can be dynamically changed any time the header.tpl file is included.
Also, notice the default modifier is applied to $title so in the case no title was supplied, then the text "no title" will show up instead of nothing.
Smarty facilitates a convenient way to loop over arrays of data with the section function.
Here's an example of that, and we also threw in alternating row colors via the cycle function, and we use the strip function to strip out newlines and white space.
Smarty可以使用section函数轻松的遍历数组.
这里有个例子,我们同时也用了cycle函数插入行交替颜色,使用strip函数换新的一行并留出空白.
index.php
include('Smarty.class.php');
// create object $smarty = new Smarty;
// assign an array of data $smarty->assign('name', array('bob','jim','joe','jerry','fred'));
There are many more href="http://smarty.php.net/manual/en/language.builtin.functions.php">builtin and href="http://smarty.php.net/manual/en/language.custom.functions.php">custom functions that come with Smarty, or you can make your own, again with the easy to use href="http://smarty.php.net/manual/en/plugins.php">plugin architecture.
Take note that all of the functionality of Smarty (modifiers, functions, etc.) deal exclusively with the presentation of content.
This is an important aspect of separating your presentation layer.
All application/business logic should reside in your application, whereas all presentation-related matters should be handled by the templates.
smarty速成,translated~Crash Course[Smarty速成]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