Smarty变量调节器

来源:互联网 发布:怎么看数据库端口 编辑:程序博客网 时间:2024/05/16 11:44

变量调节器

变量调节器用于变量,自定义函数和字符串。请使用‘|’符号和调节器名称应用调节器。变量调节器由赋予的参数值决定其行为。参数由符号分开。

如果你给数组变量应用单值变量的调节,结果是数组的每个值都被调节。如果你只想要调节器用一个值调节整个数组,你必须在调节器名字前加上@符号。

(1)  capitalize将变量里的所有单词首字大写。

(2)  count_characters:计算变量里的字符数。

(3)  cat:cat里的值连接到给定的变量后面。

(4)  count_paragraphs:计算变量里的段落数量。

(5)  count_sentences:计算变量里句子的数量。

(6)  count_words:计算变量里的词数。

(7)  date_format:格式化从函数strftime()获得的时间和日期。
Unix或者mysql等的时间戳记(parsable by strtotime)都可以传递到smarty

(8)  escape:用于html转码,url转码,在没有转码的变量上转换单引号,十六进制转码,十六进制美化,或者javascript转码。默认是html转码

(9):nl2br:所有的换行符将被替换成 <br />

(10)string_format:是一种格式化字符串的方法.

实例varable_modifilers.php

<?php

include ("libs/Smarty.class.php");

$smarty= new Smarty();

$smarty->reInitSmarty("demo/templates","demo/templates_c","demo/configs");

$string="'nihao' & % php. hello. ni. hao.";

$smarty->assign("cap",$string);

$smarty->assign("num",123);

$smarty->display("varable_modifiers.tpl");

?>

varable_modifilers.tpl

Capitalize功能演示<br />

原变量内容:<{$cap|escape:"htmlall"}><br/>

应用后内容:<{$cap|capitalize}><br />

count_sharacters功能演示:<br />

<{$cap}>的字符个数为(不计空格)<{$cap|count_characters}><br/>

<{$cap}>的字符个数为(计空格)<{$cap|count_characters:true}><br/>

cat功能演示<br/>

给字符串<{$cap}>链接***之后<{$cap|cat:"***"}><br />

count_paragraphs功能演示<br />

<{$cap}>的段落数为<{$cap|count_paragraphs}><br />

count_sentences功能演示<br />

<{$cap}>的句子数为<{$cap|count_sentences}><br />

count_words功能演示<br />

<{$cap}>的单词数为<{$cap|count_words}><br />

利用date_foemat格式化当前系统日期/时间:<br />

当前系统时间:<{$smarty.now|date_format:'%Y-%m-%d%H:%M:%S'}><br/>

<{$num}>通过%md格式化<{$num|string_format:"%d"}><br/>

<{$num}>通过%m.nf格式化<{$num|string_format:"%.2f"}><br/>

<{$num}>通过%e格式化<{$num|string_format:"%4.1e"}><br/>
原创粉丝点击