注释:
{* Some programming note *}
变量修饰符
{$var | modifier}
1.首字母大写:capitalize函数把变量中所有单词的首字母变为大写。实例如下:
$smarty = new Smarty();
$smarty->assign("title", "snow expected in northeast");
$smarty->display("article.tpl");
article.tpl模板包含:
{$title|capitalize}
返回如下:
Snow Expected In Northeast
2.单词计数:count_words函数统计变量中的单词总数。实例如下:
$smarty = new Smarty();
$smarty->assign("title", "Snow Expected in Northeast.");
$smarty->assign("body", "More than 12 inches of snow is expected to
accumulate overnight in Now York.");
$smarty->display("countwords.tpl");
countwords.tpl模板包含:
<strong>{$title}</strong>({$body|count_words} words)<br/>
<p>{$body}</p>
返回如下:
<strong>Snow Expected in Northeast</strong>(14 words) <br/>
<p>More than 12 inches of snow is expected to accumulate overnight in New York.</p>
3.格式化日期:date_format
$smarty = new Smarty();
$smarty->assign("title", "Snow Expected in Northeast");
$smarty->assign("filed", "1279398890");
$smarty->display("dateformat.tpl");
dateformat.tpl模板包含:
<strong>{$title}</strong><br/>
Submitted on: {$filed|date_format:"%B %e, %Y"}
返回如下:
<strong>Snow Expected in Northeast</strong><br/>
Submitted on : July 17, 2010
4.赋默认值:当应用层没有返回值时,default函数为指示特定变量的默认值提供了一种简单的方式。
$smarty = new Smarty();
$smarty->assign("title", "Snow Expected in Northeast");
$smarty->display("default.tpl");
default.tpl模板包含:
<strong>{$title}</strong><br/>
Author:{$author|default:"Anonymous"}
返回如下:
<strong>Snow Expected in Northeast</strong><br />
Author : Anonymous
5.删除标记标签: strip_tags函数删除变量字符串中的标记标签
$smarty = new Smarty();
$smarty->assign("title", "Snow <strong>Expected</strong> in Northeast");
$smarty->display("striptags.tpl");
striptags.tpl模板包含:
<strong>{$title|strip_tags}</strong>
返回如下:
<strong>Snow Expected in Northeast</strong>
6.截取字符串:truncate函数将变量字符串截取为指定数量的字符
$summaries = array(
"Snow expected in the No over the weekend.",
"Sunny and warm weather expected in Hawaii.",
"Softball-sized hail reported in Wisconsin.",
);
$smarty = new Smarty();
$smarty->assign("summaries", $summaries);
$smarty->display("truncate.tpl");
truncate.tpl模板包含:
{foreach $summaries as $summary}
{$summary|truncate:35:"...."} <br/>
{/foreach}
返回结果如下:
Snow expected in the Northeast...<br/>
Sunny and warm weather expected...<br/>
Softball-sized hail reported in ...<br/>
2.控制结构:
1.if函数
Smarty的if函数与PHP语言中的if函数相同。与PHP一样,可以使用一些条件限定符
eq le is not odd ==
Gt Ne div by !=
Gte Neq even by >
Ge is even not <
Lt is not even mod <=
lte is odd odd by >=
下面是一个简单实例:
{*Assume $dayofweek = 6.*}
{if $dayofweek > 5}
<p>Gotta love the weekend!</p>
{/if}
假设希望根据月份插入某个消息。如下实例使用了条件限定符,elseif以及else语句来完成这个任务:
{if $month < 4}
Summer is coming!
{elseif $month ge 4 && $month <= 9}
It's hot out today!
{else}
Brrr... It's cold!
{/if}
Note: 把条件语句包围在大括号中是可选的,但在标准PHP代码中这却是必须的
2.foreach函数
foreach函数与PHP语言中的同名结果做法相同。考虑一个例子。假设希望循环处理一周中的每一天:
$smarty = new Smarty();
$dayofweek = array("Mon.", "Tues.", "Weds.", "Thurs.", "Fri.", "Sat.", "Sun.");
$smarty->assign("dayofweek", $dayofweek);
$smarty->display("dayofweek.tpl");
dayofweek.tpl模板包含:
{foreach $daysofkweek as $day}
{$day} <br />
</foreach>
这会返回如下结果:
Mon.<br/>
Tues.<br/>
Weds.<br/>
Thurs.<br/>
Fri.<br/>
Sat.<br/>
Sun.<br/>
可以使用foreach循环迭代处理一个关联数据。
$smarty = new Smarty();
$states = array("OH" => "Ohio", "CA" => "California", "NY" => "New York");
$smarty->assign("states", $states);
$smarty->display("states.tpl");
states.tpl模板包含:
{foreach $states as $key=>$item}
{$key}:{$item} <br/>
{/foreach}
这会返回:
OH:Ohio<br/>
CA:California<br/>
NY:New York<br/>
Note:虽然foreach函数非常有用,但绝对需要花些时间学习另一个功能与之类似的函数section.这个函数功能更加强大
3.foreachelse函数
foreachelse函数与foreach一起使用,与用于字符串的default标签作用类似,数组为空时foreachelse
函数可以生成某个候选输出。
{foreach $states as $key => $item}
{$key} : {$item} <br/>
{foreachelse}
<p>No states matching your query were found.</p>
</foreach>
Note:foreachelse不使用结束括号,它嵌入到foreach中,这与elseif嵌入到if函数中很类似
4.section函数
section函数的操作就像是一个改进的for/foreach语句,它会迭代处理并处理数据输出,但其语法差别很大。
这里改进一词是指它与for/foreach结构提供了相同的循环特性,另外还提供了很多附加选项,可以更多地控制
循环的执行。这些选项要通过函数参数来支持。
有两个参数是必要的,如下所示
name:确定节的名。节名可以任意,应当设置为能够描述节的目的的任意名字
loop:设置循环迭代的次数。应当设置为与数组变量同名
还有几个可选参数
start:确定迭代开始的索引位置。如果数组包含5个值,而start设置为3,则迭代将从数组的索引3开始,
如果给出的是负值,起始位置由从数组末尾减去该数字来确定
step: 确定在数组中移动的步长值。默认情况下,这个值为1。设置step为3将导致迭代在数组索引0、3、6、9
等处发生。设置step为负值将导致迭代从数组末尾向前进行
max:确定迭代的最大次数
show:确定是否确实显示此节
$smarty = new Smarty();
$titles = array(
"Pro PHP",
"Beginning Python",
"Pro MySQL"
);
$smarty->assign("titles", $titles);
$smarty->display("titles.tpl");
titles.tpl模板包含:
{section name=book loop=$titles}
{$titles[book]}<br/>
</section>
这会返回
Pro PHP <br/>
Beginning Python <br/>
Pro MySQL <br/>
2.关联数组
$smarty = new Smarty();
// 创建数组
$titles[] = array(
"title" => "Pro PHP",
"author" => "Kevin McArthur",
"published" => "2008"
);
$titles[] = array(
"title" => "Beginning Python",
"author" => "Magnus Lie Hetland",
"published" => "2005"
);
$smarty->assign("titles", $titles);
$smarty->display("section2.tpl");
section2.tpl模板包含:
{section name=book loop=$titles}
<p>Title:{$titles[book].title}<br/>
Author:{$titles[book].author}<br/>
Published:{$titles[book].published}
</p>
结果返回:
<p>Title:Pro PHP<br/>
Author:Kevin McArthur<br/>
Published:2008</p>
<p>Title:Beginning Python<br/>
Author:Magnus Lie Hetland<br/>
Published:2005</p>
5.sectionelse函数
sectionelse函数与section一起使用
{section name=book loop=$titles}
{$titles[book]}<br/>
{sectionelse}
<p>No entries matching your query were found.</p>
</section>
语句:
1.include语句
2.insert语句
3.literal语句
literal语句告诉Smarty:标签中嵌入的任何数据都应当原样输出,不需要转换
<html>
<head>
<title>Welcome, {$user} </title>
{literal}
<style type="text/css">
p {
margin: 5px;
}
</style>
</literal>
</head>
</html>
6.创建配置文件
以下是个示例配置文件 app.config
# Global Variables
appName = "Example.com New2 Service"
copyright = "Copyright 2008 Example.com News Service, Inc."
[Aggregation]
title = "Recent News"
warning = """Copyright warning.Use of this information is for
Personal use only ... """
[Detail]
title = "A Closer Look..."
配置文件存储在configs目录中,并使用Smarty函数config_load加载。
$smarty = new Smarty();
$smarty->configLoad("app.config");
缓存:
<?php
require("Smarty.class.php");
$smarty = new Smarty();
$smarty->caching = 1;
$smarty->display("news.tpl");
?>
处理缓存生命期
<?php
require("Smarty.class.php");
$smarty = new Smarty();
$smarty->caching = 1;
// 将缓存生命期设为30分钟
$smarty->cache_lifetime = 1800;
$smarty->display("news.tpl");
?>
通过is_cached()消除处理开销
<?php
require("Smarty.class.php");
$smarty = new Smarty();
$smarty->caching = 1;
if (!$smarty->isCached("lottery.tpl")) {
if (date('l') == "Tuesday") {
$random = rand(100000, 999999);
}
}
$smarty->display("lottery.tpl");
?>
为每个模板创建多个缓存
<?php
require("Smarty.class.php");
require("boxer.class.php");
$smarty = new Smarty();
$smarty->caching = 1;
try {
// 如果模板未缓存,检索适当的信息
if (!is_cached("boxerbio.tpl", $_GET['boxerid'])) {
$bx = new boxer();
if (! $bx->retrieveBoxer($_GET['boxerid'])) {
throw new Exception("Boxer not found");
}
// 创建正确的Smarty变量
$smarty->assign("name", $bx->getName());
$smarty->assign("bio", $bx->getBio());
}
/*呈现并缓存模板,为它分配$_GET['boxerid']表示的名字。如果已缓存,则获取缓存的模板*/
$smarty->display("boxerbio.tpl", $_GET['boxerid']);
} catch (Exception $e) {
echo $e->getMessage();
}
?>
?>