正在加载文档...
iCMS 可用模板方法列表
iCMS 模板方法说明文档
iCMS 提供了丰富的模板方法,包括 调节器、块处理器 和 函数处理器,用于在模板中对数据进行处理和展示。以下是各类模板方法的详细说明。
1. 调节器 (tpl.modifier 开头)
调节器用于对模板变量进行处理,形式为 <!--{$变量|调节器}-->。调节器可以带参数,参数可以是模板自带的调节器,也可以是 PHP 函数。
| 调节器名称 | 功能描述 | 示例用法 |
|---|---|---|
as |
将值赋值给指定的模板变量 | <!--{$value\|as:'key'}--> |
assign |
将值赋值给指定的模板变量 | <!--{$value\|assign:'key'}--> |
call |
调用对象的方法或静态方法 | <!--{$object\|call:'method':'arg1':'arg2'}--> |
cnlen |
计算字符串的长度(支持中文字符) | <!--{$str\|cnlen}--> |
cut |
截取字符串,支持中文字符 | <!--{$str\|cut:10:'...'}--> |
date |
格式化日期 | <!--{$timestamp\|date:'Y-m-d H:i:s'}--> |
encrypt |
对字符串进行加密 | <!--{$str\|encrypt}--> |
fields |
从数组中提取指定字段 | <!--{$array\|fields:'key1,key2'}--> |
html2txt |
将 HTML 转换为纯文本 | <!--{$html\|html2txt}--> |
htmlcut |
截取 HTML 内容,保留 HTML 标签 | <!--{$html\|htmlcut:300}--> |
htmldecode |
将 HTML 实体转换为字符 | <!--{$html\|htmldecode}--> |
json |
将数据转换为 JSON 格式 | <!--{$data\|json}--> |
json_param |
将 JSON 数据转换为 URL 参数格式 | <!--{$json\|json_param}--> |
key2num |
将数组的键转换为数字 | <!--{$array\|key2num}--> |
keys |
从数组中提取或移除指定键 | <!--{$array\|keys:'key1,key2':true}--> |
markdown |
将 Markdown 内容转换为 HTML | <!--{$markdown\|markdown}--> |
pinyin |
将中文字符转换为拼音 | <!--{$str\|pinyin:'-'}--> |
pluck |
从数组中提取指定键的值 | <!--{$array\|pluck:'key'}--> |
print_r |
打印数组或变量的结构(用于调试) | <!--{$array\|print_r}--> |
random |
生成随机字符串 | <!--{$length\|random:1}--> |
sprintf |
格式化字符串 | <!--{'%s-%s'\|sprintf:'arg1':'arg2'}--> |
thumb |
生成缩略图 | <!--{$image\|thumb:100:100}--> |
unicode |
将字符串转换为 Unicode 编码 | <!--{$str\|unicode}--> |
upper |
将字符串转换为大写 | <!--{$str\|upper}--> |
capitalize |
将字符串中每个单词的首字母大写 | <!--{$str\|capitalize}--> |
cat |
将多个字符串拼接在一起 | <!--{$str\|cat:'追加内容'}--> |
count_characters |
计算字符串的字符数(可选是否包含空格) | <!--{$str\|count_characters:true}--> |
count_paragraphs |
计算字符串中的段落数 | <!--{$str\|count_paragraphs}--> |
count_sentences |
计算字符串中的句子数 | <!--{$str\|count_sentences}--> |
count_words |
计算字符串中的单词数 | <!--{$str\|count_words}--> |
default |
为空的变量提供默认值 | <!--{$var\|default:'默认值'}--> |
escape |
对字符串进行转义(支持多种转义类型) | <!--{$str\|escape:'html'}--> |
explode |
将字符串按指定分隔符拆分为数组 | <!--{$str\|explode:','}--> |
indent |
对字符串的每一行进行缩进 | <!--{$str\|indent:4:'\t'}--> |
lower |
将字符串转换为小写 | <!--{$str\|lower}--> |
preg_replace |
使用正则表达式进行字符串替换 | <!--{$str\|preg_replace:'/pattern/':'replacement'}--> |
print_var |
格式化变量内容并输出(用于调试) | <!--{$var\|print_var}--> |
replace |
替换字符串中的指定内容 | <!--{$str\|replace:'old':'new'}--> |
spacify |
在字符串的每个字符之间插入空格 | <!--{$str\|spacify:' '}--> |
string_format |
格式化字符串(类似sprintf) |
<!--{'%s-%s'\|string_format:'arg1':'arg2'}--> |
strip |
去除字符串中的多余空格和换行 | <!--{$str\|strip}--> |
truncate |
截取字符串,超出部分用省略号表示 | <!--{$str\|truncate:50:'...'}--> |
2. 块处理器 (tpl.block 开头)
块处理器用于处理模板中的块内容,形式为 <!--{block}-->内容<!--{/block}-->。
| 块处理器名称 | 功能描述 | 示例用法 |
|---|---|---|
cache |
缓存块内容,减少重复渲染 | <!--{cache id="key" time="3600"}-->内容<!--{/cache}--> |
capture |
捕获块内容并存储到变量中 | <!--{capture name="var"}-->内容<!--{/capture}--> |
strip |
去除块内容中的多余空格和换行 | <!--{strip}-->内容<!--{/strip}--> |
textformat |
格式化块内容(如缩进、换行等) | <!--{textformat style="email"}-->内容<!--{/textformat}--> |
3. 函数处理器 (tpl.function 开头)
函数处理器用于在模板中调用函数,形式为 <!--{function 参数}-->。
| 函数处理器名称 | 功能描述 | 示例用法 |
|---|---|---|
array |
创建或操作数组 | <!--{array as="arr" key="value"}--> |
exit |
终止模板渲染 | <!--{exit}--> |
in_array |
检查数组中是否存在指定值 | <!--{in_array array=$array match="value" returnvalue="存在"}--> |
rand |
从指定值中随机选择一个 | <!--{rand values="1,2,3" as="random"}--> |
counter |
计数器,用于生成递增或递减的数字 | <!--{counter start=1 skip=2}--> |
cycle |
循环遍历一组值 | <!--{cycle values="1,2,3"}--> |
math |
执行数学运算 | <!--{math equation="x + y" x=1 y=2}--> |
4. 模板输出清理 (tpl.output.clean)
| 功能名称 | 功能描述 | 示例用法 |
|---|---|---|
clean |
清理模板输出,去除多余的空格和换行 | <!--{clean}--> |
5. 使用示例
5.1 调节器示例
<!-- 格式化日期 -->
<!--{$timestamp|date:'Y-m-d H:i:s'}-->
<!-- 截取字符串 -->
<!--{$str|cut:10:'...'}
<!-- 调用对象方法 -->
<!--{$object|call:'method':'arg1':'arg2'}-->
<!-- 将字符串转换为大写 -->
<!--{$str|upper}
<!-- 截取字符串 -->
<!--{$str|truncate:50:'...'}-->
<!-- 替换字符串 -->
<!--{$str|replace:'old':'new'}-->
5.2 块处理器示例
<!-- 缓存块内容 -->
<!--{cache id="key" time="3600"}-->
<div>缓存内容</div>
<!--{/cache}-->
<!-- 捕获块内容 -->
<!--{capture name="var"}-->
<div>捕获的内容</div>
<!--{/capture}-->
{$var}
<!-- 去除多余空格和换行 -->
<!--{strip}-->
<div>
内容
</div>
<!--{/strip}-->
5.3 函数处理器示例
<!-- 创建数组 -->
<!--{array as="arr" key="value"}-->
<!-- 随机选择值 -->
<!--{rand values="1,2,3" as="random"}-->
<!-- 计数器 -->
<!--{counter start=1 skip=2}-->
<!-- 循环遍历值 -->
<!--{cycle values="1,2,3"}-->
<!-- 数学运算 -->
<!--{math equation="x + y" x=1 y=2}-->
6. 最佳实践
-
调节器的使用:
- 调节器可以链式调用,如
<!--{$str|cut:10|html2txt}-->。 - 调节器的参数可以是变量或常量,如
<!--{$str|cut:$len}-->。
- 调节器可以链式调用,如
-
块处理器的使用:
- 块处理器适合用于缓存频繁渲染的内容,如列表页、详情页等。
- 缓存时间应根据内容的更新频率合理设置。
-
函数处理器的使用:
- 函数处理器适合用于生成动态内容,如随机值、数组操作等。
- 函数处理器的参数应尽量简洁,避免复杂的逻辑。
通过合理使用模板方法,开发者可以高效地处理模板中的数据,提升模板的可读性和可维护性。