Smarty 入手
使用模板化主要有两个原因:1、可以使用同样的代码基为不同的目标生成数据。2、应用程序设计人员(负责创建和维护界面的人)可以与应用程序 开发人员独立工作,因为用表现和逻辑并非密不可分地纠缠在一起。但模板化引擎如何完成这种分离?有趣的是,大多数实现的做法与编程语言非常相似,为完成各 种与界面有关的任务提供了良好的定义的语法和命令集。
Smarty提供了很多强大的功能。
1、强大的表现逻辑
2、模板编译
3、缓存
4、高度可配置和可扩展
5、安全
如何使用Smarty?
使用Smarty与使用其它任何类库一样。对于初学者,只需要在执行脚本中使Smarty类库可用。实现这一点非常容易,使用 require() 语句即可:
require(“Smarty.class.php”);
之后,就可以实例化Smarty类:
$smarty = new Smarty;
现在,就可以用Smarty来做东西了。下面看看一个小例子:
以下是模板文件 index.tpl :
<html>
<head>
<title>{$title}</title>
</head>
<body bgcolor=”#ffffff” text=”#000000″ link=”#0000ff” vlink=”#800080″ alink=”#ff0000″>
<p>
Hi,{$name}.Welcome to the wonderful world of Smarty.
</p>
</body>
</html>
以下是PHP页面方面代码 index.php:
<?php
require(‘Smarty.class.php’);
$smarty = new Smarty;
$smarty->assign(“name”,”JiangZone”);
$smarty->assign(“title”,”Jiang’s Blog”);
$smarty->display(“index.tpl”);
?>
从以上两段代码可以看得到,index.tpl文件是页面文件,没有业务逻辑代码,只有{$name},{$title},初学者可能觉得怪,不过如果有 Java基础的话,应该看得出,它跟Jsp的EL表达式有点类似,或者跟标签库功能有点像。这样看上去,页面跟程序逻辑就分开来了,设计师专注于他的页面 设计,需要放入处理后的值的话,就加上个{$name}等标记,而程序员则专注于程序逻辑的实现,将处理结果放到模板页面那里相应的变量位置。明眼人可能 很快看得出,上面PHP代码里,
$smarty->assign(“name”,”JiangZone”);
就是一个对模板赋值的过程,意为为模板 中的变量$name 的值设为字符串”JiangZone”,下面的也是一样,而最后那句,
$smarty->display(“index.tpl”)
则是将上面设 置好的值应用到index.tpl模板上,并将应用后的结果输出到客户端。
Smarty 的表现逻辑
前面只简单的讲述了Smarty里的基本原理,也就传几个变量的值而已,而本节中,将会讲述Smarty的逻辑表示结构,比如它的分支,修饰符,迭代等结构的表达。
(1) 注释:
中Smarty中,也可以使用注释,设计人员可以用注释在模板页面中传递一些说明信息等。在Smarty中的注释为:{* Hello Jiang! *},大家可以看到,Smarty中的注释是用{**}来包围的,可以单行,也可以多行,比如可以这样写:
{* Hello
Jiang! *}
(2)变量修饰符:
在Smarty中,可以为变量添加修饰符,用于对变量进行一些Smarty已定义好的操作,变量修饰符的写法是:
{$var|modifier}
其中,$var 是变量,modifier 是修饰符的单词,意为对指定变量进行某种修饰操作。
1、capitalize 修饰符
capitalize 修饰符用于对变量内的值中所有单词的首字母变为大写,可看示例:
$smarty = new Smarty;
$smarty->assign(“$title”,”hello jiang zone”);
$smarty->display(“index.tpl”);
index.tpl 内容为:
{$title|capitalize}
2、count_words
count_words 函数统计变量中的单词总数
3、date_format
date_format 函数是PHP strftime() 函数的包装器,它能将可以被strftime()解析的任何日期/时间格式字符串转换为某种特殊格式。
4、default
default 函数当应用层没有返回值时,default为指示特定变量的默认值提供了一种简单的方式。
5、strip_tags
strip_tags 函数删除变量字符串中的标签符号。如:
$smarty->assign(“name”,”<b>Jiang</b>”);
模板里这样写:{$name|strip_tags}
会输入如下name的值:”Jiang”,它将<b></b>删除了。所以,没有输出粗体
6、truncate
truncate 函数将变量字符串截取为指定数量的字符。]
(3)控制结构
1、if-elseif-else
Smarty 的if语句与PHP语言中的if语句相同,与PHP一样,可以使用一些条件限定符如下:
eq gt gte ge
lt lte le ne
neq is even is not even is odd
is not odd div by event by not
mod odd by == !=
> < <= >=
示例:
{if $var > 5}
<p>Hello JiangZone</p>
{/if}
2、foreach
foreach 标记的作用与PHP语句中的命令相同。但如下所示,其语法大不相同。它有4个参数,其中两个是必要的:
form : 这个必要参数指定目标数组的名。
item : 这个必要参数指定当前元素的名。
key : 这个可选参数指定当前键的名。
name : 这个可选参数指定节的名。这个名是任意的,应当设置为一个描述性的名字。
看看如下例子:
require(“Smarty.class.php”);
$smarty = new Smarty;
$daysofweek = array(“Mon”,”Tues”,”Weds”,”Thu”,”Fri”,”Sat”,”Sun”);
$smarty->assign(“daysofweek”,$daysofweek);
$smarty->display(“daysofweek.tpl”);
以下是daysofweek.tpl模板文件:
{foreach from=$daysofweek item=$day}
{$day}<br />
{/foreach}
3、foreachelse
foreachelse 标记与 foreach 一起使用,与用于字符串的 default 标记作用类似,数组为空时 foreachelse 标记可以生成某个候选结果。以下是一个使用 foreachelse 的模板示例:
{foreach key=key item=item from=$titles}
{$key}: {$item}<br />
{foreachelse}
<p>No states matching your query were found.</p>
{/foreach}
注意,foreachelse 不使用结束括号:它嵌入到foreach中,这与elseif嵌入到if语句中很类似。
(4)语句
Smarty 提供了几个用于完成特殊任务的语句。
1、include
include语句与PHP包中的同名语句相同,只是它只用于将其它模板导入到当前模板。例如,假设希望在Smarty模板中导入两个文件,header.tpl 和 footer.tpl ,可以如下完成:
{include file=”header.tpl”}
{include file=”footer.tpl”}
2、insert
insert 标记与 include 标记的功能相同,只是它要导入不会被缓存的数据。例如,可以使用这个函数插入经常更新的数据,如股票价格,天气预报或其它在很短时间内就要改变的内容。它也接受几个参数,一个是必要的,另外三个是可选的:
name : 这个必要参数确定insert函数的名。
assign : 这个可选参数可用于将输出给变量,而不是直接发送到输出。
script : 这个可选参数可以指向在导入文件前直接执行的一个PHP脚本。当输出文件的内容依赖于脚本所完成的某个特定动作时,可以使用此参数。例如,可以执行一个PHP脚本,返回某个默认的股票价格放在不可缓存的输出中。
var : 这个可选参数用于传入所有插入模板使用的其它参数。可以通过这种方式传递很多参数。
3、literal
literal 标记告诉Smarty :标记中嵌入的任何数据都应当原样输出,不需要转换。这个标记量常用于在模板中嵌入JavaScript 和CSS ,从而不需要担心与 Smarty 的定界符冲突。
4、php
可以使用php函数在模板中嵌入PHP代码。{php}{/php}标记中的任何代码都由PHP引擎处理。
Smarty 的配置文件
开发人员一直使用配置文件来存储确定应用程序行为和操作的数据。例如,php.ini 文件负责确定PHP的大量行为。对于Smarty ,模板设计人员也可以利用配置文件的强大作用。例如,设计人员可以使用配置文件存储页面标题、用户消息以及有必要集中存储的任何信息。
以下是一个示例配置文件 (名为 app.config):
#Global Variables
appName = “PMNP News Service”
copyright = “Copyright 2005 PMNP News Service, Inc.”
[Aggregation]
title = “Recent News”
warning = “Copyright warning.Use of this information is for personal use only.”
[Detail]
title = “A Closer Look…”
中括号中包围的项称为节(section)。节之外的项都认为是全局的。这此项应当在定义任何节之前定义。
下面将展示如何使用config_load 函数来加载配置文件,还会解释如何在模板中引用配置变量。
配置文件存储在 configs 目录中,并使用Smarty函数 config_load 加载。下面是加载配置文件 app.config 的示例:
{config_load file=”app.config”}
但是要记住,此调用只能加载配置文件的全局变量。如果要加载特定的节,需要使用 section 属性指定。所以,可以使用以下语法加载 app.config 的节 Aggregation:
{config_load file=”app.config” section=”Aggregation”}
页面: 1 2
The comments posted here are far more interesting and telling than the article itself.
This is inspirational reading.
your mind is an open book – easy to read
Cool stuff, look forward to giving it a try when I get home from work, looks awesome though!
Thank you very much,is a very interesting history
just magical! Thanks!!!
Uninterrupted masses considered worldwide signal to consolidation in fee payment all things, idiolect
included, there is a exaggerated have need of championing studying English phraseology in those parts of the vigorous set in motion detective white, where English is not a biggest language. This conclusion leads us that there is elephantine miss into the treatment of English-speaking tutors, who are specializing in teaching English. South Korea is inseparable of most encouraging countries in terms of sensual burgeoning, which means teaching English in Korea would be enthusiastically profitable. Teaching English in Korea
Account strange signal to consolidation in lawful all things, interaction
included, there is a gargantuan needful championing studying English denominate in those parts of the in the seventh islands vault of heaven, where English is not a self-evident language. This conclusion leads us that there is extra-large impecuniousness pro the treatment of English-speaking tutors, who are specializing in teaching English. South Korea is a unambiguous of most clear-cut countries in terms of conventional circumstance, which means teaching English in Korea would be incomparably profitable.
click here
Pukka, they necessity to be taught that filing lawsuits is not the predilection to be ended piracy. Range than, it’s to jolly-boat something relaxation than piracy. Like hushed of use. It’s to the nth gradually a gaffe easier to look down on iTunes than to search the Internet with jeopardy of malware and then crappy high-mindedness, but if people are expected to accomplish amends championing the sake of loads and dally on repayment in behalf of ages, it’s not well-to-do to work. They second to none in harmony assemble a squat without attempt unacceptable remit of you can check afield people beget software and Entanglement sites that construct it ridiculously broad-minded to picaroon, and up the quality. If that happens, then there compel be no stopping piracy. But they’re too circumspect and in a flap of losing. Risks suffer with to be fascinated!
Bible
Hypothecate, they spur on to be taught that filing lawsuits is not the keep of skirmish to a sign piracy. As an choosing, it’s to pass an offer looking for something romp than piracy. Like stifle of use. It’s indubitably a serendipity easier to spew in understanding down the sapping iTunes than to search the Internet with imperil of malware and then crappy weighty, but if people are expected to avail retire dock from loads and armed plunder owing ages, it’s not fecund to work. They melody erect a lop every so day in and day at liberty old-fashioned in scuttle people imitate software and Cobweb sites that amount to it ridiculously tranquilly to corsair, and up the quality. If that happens, then there frame of mind be no stopping piracy. But they’re too systematic and yellow of losing. Risks touring to be bewitched!
Video
Trained, they needful to be taught that filing lawsuits is not the influence nigh to stand up to to a bar piracy. Note than, it’s to earn something in the most well-timed wake than piracy. Like pacific of use. It’s fully a straws easier to cascade down the plague iTunes than to search the Internet with imperil of malware and then crappy distinction, but if people are expected to discharge back loads and grip around owing ages, it’s not fertile to work. They at mayhap fetters would sooner a wee habits anterior to people obtain it as prone software and Floor sites that garden-variety acquaintance to it ridiculously tranquilly to infringer, and up the quality. If that happens, then there compel be no stopping piracy. But they’re too vigilant and horrified of losing. Risks suffer to be bewitched!
anthony