技巧和窍门
空变量处理
有时你可能希望为一个空的变量打印一个默认值,而不是不打印任何东西,例如打印和nbsp;
,以便 HTML 表格背景能够正常工作。许多人会使用 {if}
语句来处理这个问题,但有一个使用 Smarty 的简便方法,即使用 default
变量修改器。
注意
如果未在 PHP 的
error_reporting()
级别或 Smarty 的$error_reporting
属性中禁用“未定义变量”错误,并且尚未向 Smarty 分配变量,则会显示 E_NOTICE。
{* the long way *}
{if $title eq ''}
{else}
{$title}
{/if}
{* the short way *}
{$title|default:' '}
默认变量处理
如果一个变量在你的模板中频繁使用,则每次提到它时都应用 default
修改器可能会变得有点难看。你可以通过使用 {assign}
函数将变量分配给其默认值来补救这种情况。
{* do this somewhere at the top of your template *}
{assign var='title' value=$title|default:'no title'}
{* if $title was empty, it now contains the value "no title" when you use it *}
{$title}
将变量标题传递至页眉模板
当大多数模板使用相同的页眉和页脚时,通常做法是将它们拆分为自己的模板,并 {include}
它们。但如果你需要为根据你所访问的页面,让页眉拥有不同的标题?这可以通过一个 属性,当你包含它的时候将标题传给页眉。
mainpage.tpl
- 当绘制主页面时,“主页面”标题将传给 header.tpl
,并随后用作标题。
{include file='header.tpl' title='Main Page'}
{* template body goes here *}
{include file='footer.tpl'}
archives.tpl
- 在绘制存档页时,标题将是“存档”。在存档示例中,我们使用来自 archives_page.conf
文件的变量,而不是硬编码变量。
{config_load file='archive_page.conf'}
{include file='header.tpl' title=#archivePageTitle#}
{* template body goes here *}
{include file='footer.tpl'}
header.tpl
- 注意,如果未设置 $title
变量,将打印“Smarty 新闻”,使用 default
变量修饰符。
footer.tpl
日期
根据经验,始终以 时间戳的形式将日期传递给 Smarty。这允许模板设计人员使用 date_format
修饰符完全控制日期格式,并且还可以轻松比较日期(如有必要)。
这将输出
这将输出
可以使用时间戳在模板中比较日期
在模板中使用 {html_select_date}
时,程序员很可能希望将表单中的输出转换回时间戳格式。这里有一个函数可以帮助你解决这个问题。
<?php
// this assumes your form elements are named
// startDate_Day, startDate_Month, startDate_Year
$startDate = makeTimeStamp($startDate_Year, $startDate_Month, $startDate_Day);
function makeTimeStamp($year='', $month='', $day='')
{
if(empty($year)) {
$year = strftime('%Y');
}
if(empty($month)) {
$month = strftime('%m');
}
if(empty($day)) {
$day = strftime('%d');
}
return mktime(0, 0, 0, $month, $day, $year);
}
另请参见 {html_select_date}
、{html_select_time}
、date_format
和 $smarty.now
、
组件化模板
传统上,将模板编程到应用程序中的做法如下:首先,在 PHP 应用程序中累积变量(可能使用数据库查询)。然后,实例化 Smarty 对象,assign()
变量并 display()
模板。因此,例如,假设我们的模板上有股票行情。我们将在应用程序中收集股票数据,然后在模板中分配这些变量并显示它。现在,仅仅包含模板,而不必担心预先获取数据,这样不是很方便吗?
你可以通过编写一个自定义插件来获取内容并将其分配给模板变量,从而实现此目的。
function.load_ticker.php
<?php
// setup our function for fetching stock data
function fetch_ticker($symbol)
{
// put logic here that fetches $ticker_info
// from some ticker resource
return $ticker_info;
}
function smarty_function_load_ticker($params, $smarty)
{
// call the function
$ticker_info = fetch_ticker($params['symbol']);
// assign template variable
$smarty->assign($params['assign'], $ticker_info);
}
index.tpl
{load_ticker symbol='SMARTY' assign='ticker'}
Stock Name: {$ticker.name} Stock Price: {$ticker.price}
另请参见:{include}
。
混淆电子邮件地址
您是否曾疑惑过您的电子邮件地址如何出现在如此多的垃圾邮件列表中?垃圾邮件发送者收集电子邮件地址的一种方法来自网页。为帮助解决此问题,您可以使您的电子邮件地址显示在 HTML 源中打乱的 javascript 中,但它在浏览器中仍可以正确显示和使用。这是通过 {mailto}
插件完成的。
<div id="contact">Send inquiries to
{mailto address=$EmailAddress encode='javascript' subject='Hello'}
</div>
注意
这种方法并非 100% 万无一失。垃圾邮件发送者可以想象地对其电子邮件收集器进行编程以解码这些值,但可能性不大.... 希望如此..可是... 量子计算机在哪儿 :-?。