Chapter 18. Tips & Tricks


Table of Contents
Blank Variable Handling[空白变量处理]
Default Variable Handling[默认变量处理]
Passing variable title to header template[把变量型标题传递到头模板]
Componentized Templates[组合的模板]
Obfuscating E-mail Addresses[拒绝电子邮件地址]

Blank Variable Handling



There may be times when you want to print a default value for an empty variable instead of printing nothing, such as printing " " so that table backgrounds work properly. Many would use an {if} statement to handle this, but there is a shorthand way with Smarty, using the default variable modifier.

也许有时候你想给一个没有赋值的变量输出一个默认的值来代替什么也不输出,比如输出' '[这是一个空格符号,HTML里的标记]来使得表格里的背景能够正常工作。大多数人使用{if}语句来处理这些,但是用Smatry这里有一个捷径,那就是使用 default [默认的]值赋给变量。

Example 18-1. Printing   when a variable is empty

例 18-1。输出 ---如果一个变量是空值

{* the long way *}

{if $title eq ""}

{* the short way *}

{$title|default:" "}