Smarty 教程
Smarty是一个php模板引擎。更准确的说,它分开了逻辑程序和外在的内容,提供了一种易于管理的方法。可以描述为应用程序员和美工扮演了不同的角色,因为在大多数情况下 ,他们不可能是同一个人。例如,你正在创建一个用于浏览新闻的网页,新闻标题,标签栏,作者和内容等都是内容要素,他们并不包含应该怎样去呈现。在Smarty的程序里,这些被忽略了。模板设计者们编辑模板,组合使用html标签和模板标签去格式化这些要素的输出(html表格,背景色,字体大小,样式表,等等)。有一天程序员想要改变文章检索的方式(也就是程序逻辑的改变)。这个改变不影响模板设计者,内容仍将准确的输出到模板。同样的,哪天美工吃多了想要完全重做界面,也不会影响到程序逻辑。因此,程序员可以改变逻辑而不需要重新构建模板,模板设计者可以改变模板而不影响到逻辑。
现在简短的说一下什么是smarty不做的。smarty不尝试将逻辑完全和模板分开。如果逻辑程序严格的用于页面表现,那么它在模板里不会出现问题。有个建议:让应用程序逻辑远离模板, 页面表现逻辑远离应用程序逻辑。这将在以后使内容更容易管理,程序更容易升级。
Smarty的特点之一是"模板编译"。意思是Smarty读取模板文件然后用他们创建php脚本。这些脚本创建以后将被执行。因此并没有花费模板文件的语法解析,同时每个模板可以享受到诸如Zend加速器(http://www.zend.com) 或者PHP加速器(http://www.php-accelerator.co.uk)。这样的php编译器高速缓存解决方案。
Smaty的一些特点:
- 非常非常的快!
- 用php分析器干这个苦差事是有效的
- 不需要多余的模板语法解析,仅仅是编译一次
- 仅对修改过的模板文件进行重新编译
- 可以编辑'自定义函数'和自定义'变量',因此这种模板语言完全可以扩展
- 可以自行设置模板定界符,所以你可以使用{}, {{}}, <!--{}-->, 等等
- 诸如 if/elseif/else/endif 语句可以被传递到php语法解析器,所以 {if ...} 表达式是简单的或者是复合的,随你喜欢啦
- 如果允许的话,section之间可以无限嵌套
- 引擎是可以定制的.可以内嵌php代码到你的模板文件中,虽然这可能并不需要(不推荐)
- 内建缓存支持
- 独立模板文件
- 可自定义缓存处理函数
- 插件体系结构
Smarty安装
安装Smarty发行版在/libs/目录里的库文件(就是解压了). 这些php文件你可不能乱画哦.这些文件被所有应用程序共享,也只能在你升级到新版的smarty的时候得到更新。
Smarty手册范例 2-1.Smarty库文件
Smarty.class.php Smarty_Compiler.class.php Config_File.class.php debug.tpl /core/*.php (all of them) /plugins/*.php (all of them)
Smarty使用一个叫做'SMARTY_DIR'的php常量作为它的系统库目录。基本上,如果你的应用程序可以找到Smarty.class.php文件,你不需要设置SMARTY_DIR,Smarty将会自己运作。但是,如果Smarty.class.php没有在你的include_path(php.ini里的一项设置)里,或者没有在你的应用程序里设置它的绝对路径的时候,你就必须手动配置SMARTY_DIR 了(大多数程序都如此)SMARTY_DIR必须包含结尾斜杠。
这里是你在你的php脚本里创建一个smarty的应用实例的例子:
require('Smarty.class.php');
$smarty = new Smarty;
试着运行一下以上脚本,如果你发现"未找到Smarty.class.php 文件"的错误时,你应该这样做:
Smarty手册范例 2-3.加入库文件目录的绝对路径
require('/usr/local/lib/php/Smarty/Smarty.class.php');
$smarty = new Smarty;
Smarty手册范例 2-4.在include_path加入库文件目录
// Edit your php.ini file, add the Smarty library
// directory to the include_path and restart web server.
// Then the following should work:
require('Smarty.class.php');
$smarty = new Smarty;
Smarty手册范例 2-5.手工设置SMARTY_DIR常量
define('SMARTY_DIR','/usr/local/lib/php/Smarty/');
require(SMARTY_DIR.'Smarty.class.php');
$smarty = new Smarty;
现在库文件已经搞定,该是设置为你的应用程序配置其他有关Smarty的目录的时候了。Smarty要求4个目录,默认下命名为:tempalates, templates_c, configs and cache。每个都是可以自定义的,可以修改Smarty类属性: $template_dir, $compile_dir, $config_dir, and $cache_dir respectively。强烈推荐你为每个用到smarty的应用程序设置单一的目录!
确定你已经知道了你的web服务器文件根目录。在我们的例子里,文件根目录是:"/web/www.mydomain.com/docs/"Smarty的4个目录 只可以被那些库文件访问,不可以被网络上的浏览器访问的目录。因此为避免任何安全问题,要求将那4个目录和网页文件目录(就是浏览器看的)分开来。
在我们的安装例子里,我们将为一个留言板程序配置smarty环境。我们挑选应用程序只为了实现目录命名约定。你可以对任何程序使用相同的环境,只要将"guestbook"改成你要的名字就可以了。我们将把Smarty目录放在 "/web/www.mydomain.com/smarty/guestbook/"下。
在你的文档目录下至少得有一个文件,这个文件可以被浏览器访问.我们叫它 "index.php"好了.把它放到"/guestbook/"目录下.
技术提示:建立web服务器很方便,这个文件可以被web服务器自动识别。如果你访问"http://www.mydomain.com/guestbook/",你不需要在URL上输入"index.php",index.php脚本就可以被执行。在Apache服务器中,可以通过在DirectoryIndex的后面添加"index.php" 文件(用反斜杠分开每个入口)来完成设置。
现在我们看看这些文件结构:
Smarty手册范例 2-6.例子的文件结构
/usr/local/lib/php/Smarty/Smarty.class.php /usr/local/lib/php/Smarty/Smarty_Compiler.class.php /usr/local/lib/php/Smarty/Config_File.class.php /usr/local/lib/php/Smarty/debug.tpl /usr/local/lib/php/Smarty/core/*.php /usr/local/lib/php/Smarty/plugins/*.php /web/www.mydomain.com/smarty/guestbook/templates/ /web/www.mydomain.com/smarty/guestbook/templates_c/ /web/www.mydomain.com/smarty/guestbook/configs/ /web/www.mydomain.com/smarty/guestbook/cache/ /web/www.mydomain.com/docs/guestbook/index.php
Smarty的$compile_dir和$cache_dir必须可写。通常是user "nobody" 和 group "nobody"。如果是 OSX用户,默认为user "web" 和 group "web"。如果你在使用Apache,你可以看看httpd.conf 文件 (通常在"/usr/local/apache/conf/"目录下)哪些user和group正在被使用。
Smarty手册范例 2-7 文件权限设置
chown nobody:nobody /web/www.mydomain.com/smarty/guestbook/templates_c/ chmod 770 /web/www.mydomain.com/smarty/guestbook/templates_c/ chown nobody:nobody /web/www.mydomain.com/smarty/guestbook/cache/ chmod 770 /web/www.mydomain.com/smarty/guestbook/cache/
技术提示:
chmod 770相当安全了,它只让user "nobody" 和 group "nobody" 读/写 访问。如果你要对任何人开放读取访问权限(大多是为了你自己查看文件),你可以使用 775。
我们需要创建index.tpl文件让smarty载入.这个文件放在 $template_dir目录里。
Smarty手册范例 2-8 编辑/web/www.mydomain.com/smarty/templates/index.tpl
{* Smarty *}
Hello, {$name}!
技术提示:
{* Smarty *} 是一个模板注释。虽然并不是必须的,但是这可以很好的锻炼你在模板文件里加入注释的习惯。它可以使文件便于识别。例如,一些文本编辑器可以识别这个文件,并加以语法高亮显示。
现在来编辑index.php。我们将创建一个Smarty的实例,指派模板变量,显示 index.tpl文件。在我们的例子的环境里, "/usr/local/lib/php/Smarty"已经包括在了 include_path里了。
Smarty手册范例 2-9.编辑/web/www.mydomain.com/docs/guestbook/index.php
// load Smarty library
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->template_dir = '/web/www.mydomain.com/smarty/guestbook/templates/';
$smarty->compile_dir = '/web/www.mydomain.com/smarty/guestbook/templates_c/';
$smarty->config_dir = '/web/www.mydomain.com/smarty/guestbook/configs/';
$smarty->cache_dir = '/web/www.mydomain.com/smarty/guestbook/cache/';
$smarty->assign('name','Ned');
$smarty->display('index.tpl');
技术提示:
在我们的例子里,已经设置了所有Smarty目录的绝对目录。如果 '/web/www.mydomain.com/smarty/guestbook/' 已经包括在 include_path里了,那么这些设置则没有必要。但是,从经验和通用性看来,为避免发生错误,还是配置一下为好。
现在在浏览器打开 index.php,你应该看到"Hello, Porky!"
你现在已经完成了Smarty的基本设置,恭喜!!
Smarty扩展设置
扩展设置
这是基本安装的继续,请先阅读那个文件!
一个更灵活一点的配置Smarty的方法是扩展类,和初始化你的smarty环境。 为了避免重复地配置路径,我们可以在一个文件里配置这些变量。 我们创建一个目录 "/php/includes/guestbook/" 建立一个文件"setup.php" 同样先设置好smarty路径。
// load Smarty library
require('Smarty.class.php');
// The setup.php file is a good place to load
// required application library files, and you
// can do that right here. An example:
// require('guestbook/guestbook.lib.php');是一个很好的加载应用程序的类库文件(就是扩展类)
//例如你可以在index文件里包含它
class Smarty_GuestBook extends Smarty {
function Smarty_GuestBook() {
// Class Constructor. These automatically get set with each new instance.
//类构造函数.创建实例的时候自动配置
$this->Smarty();
$this->template_dir = '/web/www.mydomain.com/smarty/guestbook/templates/';
$this->compile_dir = '/web/www.mydomain.com/smarty/guestbook/templates_c/';
$this->config_dir = '/web/www.mydomain.com/smarty/guestbook/configs/';
$this->cache_dir = '/web/www.mydomain.com/smarty/guestbook/cache/';
$this->caching = true;
$this->assign('app_name','Guest Book');
}
}
现在我们针对setup文件更改一下index文件
require('guestbook/setup.php');
$smarty = new Smarty_GuestBook;
$smarty->assign('name','Ned');
$smarty->display('index.tpl');
现在你看到创建一个使用smarty的实例有多么的简单.从Smarty_GuestBook开始,重新构建我们的应用程序吧^_^
Smarty注释代码
所有的smarty模板标签都被加上了定界符.
默认情况下是 { 和},但它们是可被改变的.
例如,我们假定你在使用默认定界符.
在smarty里,所有定界符以外的内容都是静态输出的,或者称之为不可改变.
当smarty遇到了模板标签,将尝试解释他们,然后再以恰当的方式输出 .
Comments[注释]
模板注释被*号包围,例如 {* this is a comment *}
smarty注释不会在模板文件的最后输出中出现.
它只是模板内在的注释.
{* Smarty *}
{* include the header file here *}
{include file="header.tpl"}
{include file=$includeFile}
{include file=#includeFile#}
{* display dropdown lists *}
<SELECT name=company>
{html_options values=$vals selected=$selected output=$output}
</SELECT>
Smarty函数
函数
每一个smarty标签输出一个变量或者调用某种函数.
在定界符内 函数(用'{'包住)和其属性(用界符包住)将被处理和输出.例如:
{funcname attr1="val" attr2="val"}.
{config_load file="colors.conf"}
{include file="header.tpl"}
{if $highlight_name}
Welcome, <font color="{#fontColor#}">{$name}!</font>
{else}
Welcome, {$name}!
{/if}
{include file="footer.tpl"}
在模板里无论是内建函数还是自定义函数都有相同的语法.
内建函数将在smarty内部工作,例如 {if} , {section} and {strip} .他们不能被修改.
自定义函数通过插件机制起作用,它们是附加函数. 只要你喜欢,可以随意修改.你也可以自行添加.
例如 {html_options} 和 {html_select_date}
Smarty属性
属性
大多数函数都带有自己的属性以便于明确说明或者修改他们的行为.
smarty函数的属性很像HTML中的属性.
静态数值不需要加引号,但是字符串建议使用引号.
如果用变量作属性,它们也不能加引号.
一些属性用到了布尔值(真或假).
它们不需要加引号,可以是true,on,yes或者false,off,no.
{include file="header.tpl"}
{include file=$includeFile}
{include file=#includeFile#}
{html_select_date display_days=yes}
<SELECT name=company>
{html_options values=$vals selected=$selected output=$output}
</SELECT>
Smarty双引号里值的嵌入
双引号里值的嵌入
Smarty可以识别嵌入在双引号中的变量,只要此变量只包含数字、字母、下划线和中括号[].对于其他的符号(句号、对象相关的,等等)此变量必须用两个'`'(此符号和‘ ~ '在同一个键上,一般在ESC键下面一个键上)包住。
SYNTAX EXAMPLES:
{func var="test $foo test"} <-- sees $foo
{func var="test $foo_bar test"} <-- sees $foo_bar
{func var="test $foo[0] test"} <-- sees $foo[0]
{func var="test $foo[bar] test"} <-- sees $foo[bar]
{func var="test $foo.bar test"} <-- sees $foo (not $foo.bar)
{func var="test `$foo.bar` test"} <-- sees $foo.bar
PRACTICAL EXAMPLES:
{include file="subdir/$tpl_name.tpl"} <-- will replace $tpl_name with value
{cycle values="one,two,`$smarty.config.myval`"} <-- must have backticks
Smarty数学运算
数学运算可以直接应用到变量
Example 3-5. math examples
例 3-5.数学运算的例子
{$foo+1}
{$foo*$bar}
{* some more complicated examples *}
{$foo->bar-$bar[1]*$baz->foo->bar()-3*7}
{if ($foo+$bar.test%$baz*134232+10+$b+10)}
{$foo|truncate:"`$fooTruncCount/$barTruncFactor-1`"}
{assign var="foo" value="`$foo+$bar`"}
Smarty变量
变量
Smarty有几种不同类型的变量.
变量的类型取决于它的前缀是什么符号(或者被什么符号包围)
Smarty的变量可以直接被输出或者作为函数属性和修饰符(modifiers)的参数,或者用于内部的条件表达式等等.
如果要输出一个变量,只要用定界符将它括起来就可以.例如:
{$Name}
{$Contacts[row].Phone}
<body bgcolor="{#bgcolor#}">
从PHP分配的变量
内容列表
关联数组
数组下标
对象
调用从PHP分配的变量需在前加"$"符号.(译注:同php一样)
调用模板内的assign函数分配的变量也是这样.(译注:也是用$加变量名来调用)
例 4-1.分配的变量
index.php:
$smarty = new Smarty;
$smarty->assign('firstname', 'Doug');
$smarty->assign('lastLoginDate', 'January 11th, 2001');
$smarty->display('index.tpl');
index.tpl:
Hello {$firstname}, glad to see you could make it.
<p>
Your last login was on {$lastLoginDate}.
OUTPUT:
Hello Doug, glad to see you could make it.
<p>
Your last login was on January 11th, 2001.
Smarty从配置文件读取的变量
从配置文件读取的变量
配置文件中的变量需要通过用两个"#"或者是smarty的保留变量 $smarty.config.来调用(下节将讲到)
第二种语法在变量作为属性值并被引号括住的时候非常有用.
(译注:举个例子 {include file="#includefile#"} 这样#includefile#将被当作字符处理,而不表示配置文件变量,
但可以这样表示 {include file="`$smarty.config.includefile`"}不要忘了加``)
例 4-5.从配置文件引用的变量
foo.conf:
pageTitle = "This is mine"
bodyBgColor = "#eeeeee"
tableBorderSize = "3"
tableBgColor = "#bbbbbb"
rowBgColor = "#cccccc"
index.tpl:
{config_load file="foo.conf"}
<html>
<title>{#pageTitle#}</title>
<body bgcolor="{#bodyBgColor#}">
<table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}">
<tr bgcolor="{#rowBgColor#}">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
index.tpl: (alternate syntax)
{config_load file="foo.conf"}
<html>
<title>{$smarty.config.pageTitle}</title>
<body bgcolor="{$smarty.config.bodyBgColor}">
<table border="{$smarty.config.tableBorderSize}" bgcolor="{$smarty.config.tableBgColor}">
<tr bgcolor="{$smarty.config.rowBgColor}">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
OUTPUT: (same for both examples)
<html>
<title>This is mine</title>
<body bgcolor="#eeeeee">
<table border="3" bgcolor="#bbbbbb">
<tr bgcolor="#cccccc">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
配置文件的变量只有在它们被加载以后才能使用.
这个过程将在以后 {config_load} . 的章节里说明.
Smarty变量调节器
变量调节器
变量调节器用于变量,自定义函数和字符串。 请使用‘|’符号和调节器名称应用调节器。 变量调节器由赋予的参数值决定其行为。 参数由‘:’符号分开。
例 5-1.调节器的例子
{* Uppercase the title *}
<h2>{$title|upper}</h2>
{* Truncate the topic to 40 characters use ... at the end *}
Topic: {$topic|truncate:40:"..."}
{* format a literal string *}
{"now"|date_format:"%Y/%m/%d"}
{* apply modifier to a custom function *}
{mailto|upper address="me@domain.dom"}
如果你给数组变量应用单值变量的调节,结果是数组的每个值都被调节。 如果你只想要调节器用一个值调节整个数组,你必须在调节器名字前加上@符号。 例如: {$articleTitle|@count}(这将会在 $articleTitle 数组里输出元素的数目)
capitalize
将变量里的所有单词首字大写。
Example 5-2. capitalize
例 5-2.首字大写
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Police begin campaign to rundown jaywalkers.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|capitalize}
OUTPUT:
Police begin campaign to rundown jaywalkers.
Police Begin Campaign To Rundown Jaywalkers.
Smarty组合修改器
组合修改器
对于同一个变量,你可以使用多个修改器。它们将从左到右按照设定好的顺序被依次组合使用。使用时必须要用"|"字符作为它们之间的分隔符。
Example 6-1. combining modifiers
例 6-1.组合修改器
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Smokers are Productive, but Death Cuts Efficiency.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|upper|spacify}
{$articleTitle|lower|spacify|truncate}
{$articleTitle|lower|truncate:30|spacify}
{$articleTitle|lower|spacify|truncate:30:". . ."}
OUTPUT:
Smokers are Productive, but Death Cuts Efficiency.
S M O K E R S A R E P R O D U C T I V E , B U T D E A T H C U T S E F F I C I E N C Y .
s m o k e r s a r e p r o d u c t i v e , b u t d e a t h c u t s...
s m o k e r s a r e p r o d u c t i v e , b u t . . .
s m o k e r s a r e p. . .
