`

收藏的smarty总觉

阅读更多
nl2br[换行符替换成<br />]
regex_replace[正则替换]
replace[替换]
spacify[插空]
string_format[字符串格式化]
strip[去除(多余空格)]
strip_tags[去除html标签]
truncate[截取]
upper[大写]
wordwrap[行宽约束]
capitalize [首字母大写]
count_characters [计算字符数]
cat [连接字符串]
count_paragraphs [计算段落数]
count_sentences [计算句数]
count_words [计算词数]
date_format [时间格式]
default [默认]
escape [转码]
indent[缩进]
lower[小写 ]
{$name|truncate:10:"..."}
{$name|capitalize }

{include file="header.tpl"}        文件的包含
{include_php file="./header.php"}        php文件的包含
{*页面头*}                         注释,是不透明的
{$str1|capitalize}               第一句首字母要大写
{$str2|cat:"李晓军"}             变量 + 李晓军,cat为连接
{$str|date_format:"%Y年%m月%d日"}输出当前日期
{$name|default:"没有值!"}  如果变量没值就显示default定义的值
{$name|indent:8:"*"}         缩进8个空白字母位,并使用"*"取替这8个空白字符
{$name|lower}                全部变为小写

{html_select_date}     显示一个月,日, 年选择框
<{html_select_time}>
<{html_image file="/chuangye/public/images/me.jpg" alt="" border="0" width="100" height="100"}>显示图片
{html_table loop=$data cols=4 hdir="right" vdir="down" trailpad="填补空格" tr_attr="bgcolor='red'"  
table_attr='border="2"'}
{mailto address="me@domain.com" text="联系我们" cc="to@sdf.com,to2@sd.com" bcc="an@song.com" subject="我毕业了。"
extra='class="email"'}
{strip}{/strip} 标记中数据的首尾空格和回车. 这样可以保证模板容易理解且不用担心多余的空格导致问题.
{$smarty.ldelim}     显示左分隔符
<{$smarty.server.SERVER_NAME}>           取得服务器变量localhost
<{$smarty.now}>                          变量用于访问当前时间戳.
<{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S "}>      可以用 date_format调节器格式化输出
<{$smarty.const.WEBROOT}>               直接访问PHP常量
<{$smarty.template}>             显示当前被处理的模板的名字.
<{$smarty.version}>             显示smarty模板的版本
<{$news|@count}>             输出数组有多少个元素
<{$name|count_characters}> 计算变量值里的字符数.参数值boolean型决定是否计算空格数。默认不计算空格
{$name|count_paragraphs}               计算变量里的段落数量
{$name|count_sentences}               计算变量里句子的数量
{$name|count_words}               计算变量里的词数
<{$name|escape:"html"}>转码参数值为html,htmlall,url,quotes,hex,hexentity,javascrīpt。默认是html转码
{$name|nl2br}            换行符替换成<br />
{$name|regex_replace:"/\d/":"*"}         正则替换
{$name|replace:"大家好":"李晓军"} 把变量中的"大家好"替换成:李晓军
{$name|spacify:"*"}  spacify是在字符串的每个字符之间插入空格或者其他的字符串. 参数表示将在两个字符之间插入的字符串,默认为一个空格。
{$number|string_format:"%.1f"}        字符串格式化
{$name|strip}        去除重复的空格
{$text|strip_tags}        去除所有html标签
{$name|truncate:12}  截取参数1,规定截取的字符数.默认是80个.第二个参数加上什么字符.默认为...
{$text|wordwrap:10:"\n"}        超过这10个字符数换行
$smarty->config_load('my.conf');       加载配置文件,并将其中的数据传送到模板中
$smarty->clear_config();               清除所有配置变量
{debug} 将调式信息输出到页面上. 该函数是否可用取决于 Smarty 的 debug 设置. 该函数在运行时取得数据,因此不能显示使用中的模板,只能显示运行时被赋值的变量. 不过还是可以看到所有模板内当前可用的变量.
<{$SCRIPT_NAME}>  当前文件的路径

<{math equation="x + y" x=3 y=4}>
<{textformat wrap=1}>大    #$    家       好       啊。<{/textformat}>textformat 用于格式化文本. 该函数主要清理空格和特殊字符,
没有缓存的:<{$smarty.now}>

一、模板中调用php文件中函数
php文件中的方法:
function insert_get_time(){//insert 函数默认是不缓存的。并且这个属性不能修改
return date("Y-m-d H:m:s");
}
模板中调用:<{insert name="get_time"}>
还可以传递参数如下:
php端:
function insert_getBanner($array){
print_r($array);//$lid是个数组
return $array['lid'];
}
模板中调用:<{insert name="getBanner" lid='dd' sid='aa'}>

二、模板中定义变量
{assign var="UserName" value="大师兄"}在模板内部定义一个变量UserName值为“大师兄”

三、动态生成radio,checkedbox,options
php文件中的radio方法:
$tpl->assign('cust_radios', array(
1001 => 'Joe Schmoe',
1002 => 'Jack Smith',
1003 => 'Jane Johnson',
1004 => 'Charlie Brown'));
$tpl->assign('customer_id', 1001);
模板中调用:
<{html_radios name="id" options=$cust_radios checked=$customer_id separator="<br />"}>
php文件中的checkedbox方法:
$tpl->assign('cust_checkboxes', array(
1000 => 'Joe Schmoe',
1001 => 'Jack Smith',
1002 => 'Jane Johnson',
1003 => 'Charlie Brown'));
$tpl->assign('customer_id', 1001);
模板中调用:
<{html_checkboxes name="id" options=$cust_checkboxes checked=$customer_id separator="<br />"}>
php文件中的options方法:
$tpl->assign('cust_options', array(
1001 => 'Joe Schmoe',
1002 => 'Jack Smith',
1003 => 'Jane Johnson',
1004 => 'Charlie Brown'));
$tpl->assign('customer_id', 1001);
模板中调用:
<select name=customer_id>
<{html_options options=$cust_options selected=$customer_id}>
</select>


四、用{$smarty.capture.foo}调用capture中的内容
<{capture name="foo"}>呵呵。<{/capture}>
<{$smarty.capture.foo}>

五、计数器
<{counter start=0 skip=2 print=false assign=aa}>  计数器
<{$aa}>      
<{counter}><{$aa}>
<{counter}><{$aa}>


六、Cycle用来循环显示一组数值
{section name=rows loop=$data}
<tr bgcolor="{cycle values="#eeeeee,#d0d0d0"}">
<td>{$data[rows]}</td>
</tr>
{/section}



七、得到文件的内空,并赋给变量
<{fetch file="http://www.test.com/aa.html" assign="test"}> <{$test}>用来取得文件内容,并输出文件内容.

八、语法
if:
<{if $name !== ""}>
<b><{$name}></b>
<{/if}>
foreach:
<{foreach key=key item=value from=$news}><!--foreach循环数组 -->
键值:<{$key}>
内容:<{$value.id}>
<{$value.title}>
<{$value.data}><br>
<{/foreach}>
section:
<{section name=list loop=$news}>
<font color="#0000FF"><{$news
  •  
    •  
        .data}>></font><br>
        <{/section}>

        在模板中还可以执行php语句,真强。不过这点很好用的。
        <{php}>
        global $tpl;
        global $db;
        $typeArr = $tpl -> get_template_vars("value");
        $IndustryBig = $typeArr[IndustryBig];
        $db->query("SELECT Name FROM `cy_class_industry` where ID='$IndustryBig'");
        $row = $db->fetchRow();
        echo $row['Name'];
        <{/php}>

        数学算法和日期的格式化显示
        <{math equation="(x-y)/60" x=$logArr[num].logoutTime y=$logArr[num].loginTime format="%.1f"}> <font color="red">分钟</font>
    • .title}></font><font color="gray"><<{$news
  • .id}></font> <font color="#FF0000"><{$news
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics