getting start
smarty优点:
速度很快,内建缓存支持自动对模板文件进行编译,编译一次之后不需要再解析仅仅对修改过的模板文件进行重新编译可以自定义模板定界符、函数(包括缓存处理函数)、变量,编写plugin实现扩展,有完整的插件体系结构引擎本身可以定制section可以无限嵌套支持if/elseif/else/endif等流程语句
smarty init:
require('/PATH/TO/Smarty/Smarty.class.php');
/* or
define('SMARTY_DIR','/PATH/TO/Smarty/');
require(SMARTY_DIR.'Smarty.class.php'); */
/* or set include_path */
$smarty = new Smarty;
$smarty->template_dir = 'APPPATH/templates/';
$smarty->compile_dir = 'APPPATH/templates_c/';
$smarty->config_dir = 'APPPATH/configs/';
$smarty->cache_dir = 'APPPATH/cache/';
$smarty->assign('name','kinslayer');
$smarty->display('index.tpl');
/* use oo -> class Smarty_GuestBook extends Smarty {} */
smarty for template designer
定界符
默认左右定界符分别是 {和}修改默认定界符: $smarty->left_delimiter = '<!--{'; $smarty->right_delimiter = '}-->';如果不修改默认定界符,模板中的JavaScript会被转义导致最终parse失败,解决办法是修改默认定界符,或者放在{literal}{/literal}中.{literal}中的数据会被当成文本处理,模板引擎不分析.btw:尽量避免在JavaScript中用<!--和//-->来注释内容
注释
{* blablabla~ *}
变量
$开头, ""中自动解释, ''中不自动解释.""中,变量建议只包含数字、字幕、下划线和[], 如果有特殊命名的变量需要解析,可以用``来包裹,比如"test `$foo.bar` test"变量可以直接运算变量分为三种:PHP变量(Variables assigned from php) => {$var} / {$var[row].attr} / {$person->name}从配置文件中读取的预定义变量(Variables loaded from config files) => {#var#}smarty保留变量(smarty reserved variable) => {$smarty.get.page} / {$smarty.post.page} / {$smarty.cookies.username} / {$smarty.server.SERVER_NAME}smarty保留变量中包含
- Request variables
- {$smarty.get.page}
- {$smarty.post.page}
- {$smarty.cookies.username}
- {$smarty.server.SERVER_NAME}
- {$smarty.env.PATH}
- {$smarty.session.id}
- {$smarty.request.username} {* display the variable "username" from merged get/post/cookies/server/env *}
- ...
- {$smarty.now}
- {$smarty.const}
- {$smarty.capture}
- {$smarty.config}
- {$smarty.section}
- {$smarty.foreach}
- {$smarty.template}
config_load之后可以使用$smarty.config.blabla来使用配置文件中的变量
变量调节器 Variable Modifiers
对于同一个变量,你可以使用多个修改器。它们将从左到右按照设定好的顺序被依次组合使用。使用时必须要用"|"字符作为它们之间的分隔符capitalize:单词首字母大写count_characters:字符计数cat:将cat里的值连接到给定的变量后面count_paragraphs:计算段数count_sentences:计算局数count_words:计算单词数date_format:格式化时间日期default:默认值escape:用于转码,默认是html转码indent:缩进lower:纯小写nl2br:转换\n为<br />regex_replace:正则替换replace:替换spacify:在每个字符间插入空白或自定义字符string_format:格式化字符串,多用于数字strip:去除或者替换重复空格,\n,\tstrip_tags:去除<和>标签,包括在<和>之间的任何内容truncate:截取字符串upper:将变量改为全大写wordwrap:行宽,自动换行
函数
{FuncName attr1="val1" attr2="val2"}function的argv本质上是attr(attributes)内建函数和自定义函数都有相同的语法.内建函数不能被override,比如 {if},{section},{strip}自定义函数通过插件机制起作用,可以添加和修改,比如{html_options},{html_select_date}attributes的value一般要加引号(详细参见变量),布尔值(TRUE,ON,YES,FALSE,OFF,NO)和变量作为属性的话不能加引号.
内建函数 Built-in Functions
capture:保存输出在变量中config_loadforeach,foreachelseinclude:包含其他模板include_php:不建议使用insert:不缓存的输出if,elseif,elseldelim,rdelimliteralphp:不建议使用直接嵌入php脚本section,sectionelse:遍历数组strip
自定义函数 Custom Functions
assign: 显示counter: smarty内置countercycle: 轮转debugeval:将变量赋值给变量fetch:本地文件系统、HTTP或FTP上取得文件并显示文件的内容html_checkboxeshtml_imagehtml_optionshtml_radioshtml_select_datehtml_select_timehtml_tablemath:尽量在php中计算(使用了php的eval函数)mailto:自动生成电子邮件链接,可以防spampopup_initpopuptextformat:格式化文本. 主要清理空格和特殊字符,对段落按单词边界换行和行缩进等段落格式化处理
配置文件
windows ini风格,可以通过section来实现类似namespace的功能
调试控制台 Debugging Console
详见文档


Leave a comment