smarty 变量调节器(2)

来源:互联网 发布:origin画图软件中文版 编辑:程序博客网 时间:2024/05/13 13:24

1.   indent[缩进](有两个参数)

Parameter Position     Type      Required     Default                  Description
             1        integer       No           4          This determines how many characters to indent to.
                                                            决定缩进多少个字符。
             2         string       No       (one space)    This is the character used to indent with.
                                                            使用什么字符来代替缩进。


This indents a string at each line, default is 4. As an optional parameter, you can specify the number of characters to indent. As an optional second parameter, you can specify the character to use to indent with. (Use "\t" for tabs.)
在每行缩进字符串,默认是4个字符。
作为可选参数,你可以指定缩进字符数。
作为第二个可选参数,你可以指定缩进用什么字符代替。


特别提示:使用缩进时如果是在HTML中,则需要使用& n b s p;(空格)来代替缩进,否则没有效果。

例子:
index.php
$smarty = new Smarty;
$smarty->assign('a', 'ni hao');
$smarty->display('index.tpl');

index.tpl
<html>
<head>
</head>
<body>
<{$a|indent:10:"&nbsp"}>//表示缩进10个字符,缩进的内容为空格(写在html中)
</body>
</html>
输出结果:        ni hao


2. replace (两个参数 两个参数之间用“:”)
替换

Parameter Position参数位置 Type参数类型  Required必需    Default默认          Description
                1            string         Yes             n/a        This is the string of text to be replaced.
                                                                        将被替换的文本字串
                2            string         Yes             n/a        This is the string of text to replace with.
                                                                        用来替换的文本字串 


A simple search and replace on a variable.
简单的搜索和替换字符串

例子:
 index.php

$smarty = new Smarty;
$smarty->assign('a', 'ni hao');
$smarty->display('index.tpl');

index.tpl

<{$a|replace:"ni":"wo"}>

输出结果:wo hao


3.truncate


截取

   Parameter  Position参数位置    Type参数类型    Required必需    Default默认       Description描述
                      1              integer          No              80     This determines how many characters to truncate to.
                                                                                 截取字符的数量
                      2              string           No              ...    This is the text to append if truncation occurs.
                                                                                 截取后追加在截取词后面的字符串 
                      3              boolean          No             false   This determines whether or not to truncate at a word                                                                               boundary (false), or at the exact character (true).
                                                                            是截取到词的边界(假)还是精确到字符(真)


This truncates a variable to a character length, default is 80.
As an optional second parameter, you can specify a string of text to display at the end if the variable was truncated. The characters in the string are included with the original truncation length.
By default, truncate will attempt to cut off at a word boundary.
If you want to cut off at the exact character length, pass the optional third parameter of true.

从字符串开始处截取某长度的字符.默认是80个.
你也可以指定第二个参数作为追加在截取字符串后面的文本字串.该追加字串被计算在截取长度中。
默认情况下,smarty会截取到一个词的末尾。
如果你想要精确的截取多少个字符,把第三个参数改为"true"


例子:

index.php

$smarty = new Smarty;
$smarty->assign('a', 'ni hao');
$smarty->display('index.tpl');

index.tpl


truncate(截取)(截取字符串)功能演示:<br>
<{$a|truncate:10:"***":false}> 
<!--"false"是截取到词的边界(是上一个词的最后  例如:ni hao  如果截取内容为:<{$a|truncate:3:"***":false}> )  输出结果为:ni***-->
<!--"true"则精确截取字符串长度-->


4.spacify
插空

Parameter Position参数位置      Type参数类型   Required必需          Default默认          Description描述
         1                         string            No               one space     This what gets inserted between each character of the variable.
将在两个字符之间插入的字符(串)


spacify is a way to insert a space between every character of a variable. You can optionally pass a different character (or string) to insert.

插空(不知道这个词是什么意思,顾名思义了^^)是一种在字符串的每个字符之间插入空格或者其他的字符(串).

例子:
index.php

$smarty = new Smarty;
$smarty->assign('a', 'NI HAO');
$smarty->display('index.tpl');


spacify(插空)功能演示:<br>
<{$a|spacify}><br>
<{$a|lower|spacify:"***"}><!--有两个变量调节器则从左到右执行--><!--默认的插空内容为"空格"-->

输出结果: N  I     H  A  O
           n***i***   ***h***a***o

原创粉丝点击