smarty常用函数及其用法

1.smarty 截取字符串函数 truncate的用法

smarty truncate 截取字符串从字符串开始处截取某长度的字符,默认的长度为80
指定第二个参数作为截取字符串的长度
默认情况下,smarty会截取到一个词的末尾,
如果需要精确到截取多少个字符可以使用第三个参数,将其设为”true”
具体用法如下:
//index.php
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');
$smarty->display('index.tpl');
//index.tpl
{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}

输出结果:

Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after…
Two Sisters Reunite after
Two Sisters Reunite after—
Two Sisters Reunite after Eigh
Two Sisters Reunite after E…

2.smarty 中除法取整数

{(($score / $total_num)|round:4) * 100} % round:4 表示总共取的多少个数
capitalize 首字母大写
count_characters 字符计数(除空格)
count_characters:true 字符计数(包括空格)
cat:”String” 连接字符串
count_paragraphs 计算段数(\n)
count_sentences 计算句数
count_words 计算词数
date_format 格式化日期
%a – 星期缩写
%A – 星期全称
%b – 月缩写
%B – 月全称
%D – m/d/y
”%Y-%m-%d %H:%M:%S”
default 变量为空时默认输出
escape 编码: html , htmlall , url , quotes , hexentity , hex
indent 缩进 inden:缩进字数:代替字符
lower 字母小写
upper 字母大写
nl2br 换行符替换成
regex_replace 正则替换,regex_replace:”/[\r\t\n]/”:” ” 用空格替换换行和tab回车
replace 字符串替换,同PHP str_replace
spacify 在每个字符间插入字符(串),spacify值为空时插入空格
string_format 字符串格式化 string_format: “%.2f”(小数点两位) ,”%d”(整数)
strip 去除” … “ strip:”nbsp;”
strip_tags 去除html标签
truncate 截断字符串 truncate:字数:”(结束字符)”:false(截取到词边界)/true(精确到字符)
wordwrap 行宽约束 wordwrap:指定宽度:”(结束字符)”:false(截取到词边界)/true(精确到字符)

3.内建函数

{capture name=”foo”} … {/capture} 捕获模板内容,输出 {$smarty.capture.foo}{config_load fiile=”foo.txt”} 加载配置文件,输出 #变量名#{foreach form=$array key=key item=value}ID:{$key}/n Value:{$value}{/foreach}
{include file=”header.tpl” title=”#434c5d”} 包含其它模板
{include_php file=””} 包含PHP文件
{insert name=”getBanner” lid=#banner_location_id# sid=#site_id#} 动态加载函数,不缓存
必须以insert_开头的自建PHP函数,上例相当于执行PHP函数 insert_getBanner(array(‘lid’=>”,’sid’=>”))
name方法名,assign返回值保存在此变量中,script包含有此函数的PHP文件,[var ...]
{if}{elseif}{else}{/if}
{ldelim} [ ... ] {rdelim} 输出一对{ }
{literal}{/literal} 忽略其中内容(即使里面有{})
{php} [Php Script...] {/php} 直接嵌入PHP语句
{section name=list loop=决定循环次数的变量名称}
{$custid[customer]}
遍历Array循环,可嵌套多维
{sectionelse} 当无值时执行sectionelse
Not value! max[int]最大次数,show[bool]是否显示
{/section} start[int]开始位置,step[int]循环步长
{strip} [ ... ] {/strip} 去除首尾空格和回车

4.自定义函数

{assign var=”name” value=”Bob”} 用于在模板被执行时为模板变量赋值.
{counter start=起始数 skip=步长 print=false(输出) assign=输出给变量} 计数器
{cycle values=”#eeeeee,#d0d0d0″} 轮转使用一组值,可指定assign
{debug} 调试信息,output[string] html or js
{eval var=#title#} 待求值处理的变量被当作模板来处理.
{fetch file=”ftp:// OR http://”} 取得文件并显示文件的内容,支持assign
{html_checkboxes name=按钮组名 values=值数组 checked=选中状态的 根据给定的数据创建复选按钮组
output=钮组显示值 separator=”分隔按钮字符串”} 该函数可以指定哪些元素被选定
{html_image file=”pumpkin.jpg”} 产生一个图象的 HTML 标签
{html_options values=ValueArr selected=Value(Arr) output=TextArr} 根据给定的数据创建选项组
{html_radios …} 单选按钮组
{html_select_date} 自动创建日期下拉菜单. 它可以显示任意年月日.
{html_table loop=$data cols= table_attr= tr_attr=} 数组填充创建Table
{math equation=”x + y” x=$height y=$width} 在模板中进行数学计算
{mailto address=”me@domain.com“} 生成E-Mail链接
text=显示的文本,encode=hex或javascript,cc抄送,bcc暗送,subject主题,extra传递组链接的样式等
{popup_init src=”/javascripts/overlib.js”} 用于弹出窗口的函数库
{popup text=”This link takes you to my page!”}
{textformat wrap=40} [ ... ] {/textformat} 格式化文本

5.方法

append(string varname,mixed var,boolean merge) 添加
append_by_ref(string varname,mixed var,boolean merge) 引用添加(&)
assign(string varname, mixed var) 赋值给模板
assign_by_ref(string varname, mixed var) 引用赋值(&)
clear_all_assign() 清除所有赋值
clear_all_cache() 清除所有缓存
clear_assign(string var) 清除指定模板变量的值
clear_cache(‘index.tpl’) 清除指定模板的缓存
clear_compiled_tpl(‘index.tpl’) 清除已经编译的模板
clear_config() 清除配置
config_load() 加载配置
display() 输出模板内容
fetch() 取得输出内容
get_config_vars() 取配置变量的值
get_registered_object() 取得已注册的对象
get_template_vars 取得模板变量的值
is_cached(“index.tpl”) 是否已被缓存
load_filter() 加载过滤器插件
register_block() 注册一个块
register_compiler_function 注册编译函数
register_function 注册函数
register_modifier 注册修饰器
register_object 注册对象
template_exists 模板是否存在
unregister_block 注销一个块
unregister_compiler_function 注销编译函数

No Comments