zoukankan      html  css  js  c++  java
  • smarty函数转载

    Smarty常用函数  

    2009-08-13 14:05:55|  分类: Php |举报 |字号 订阅

     

    1 .include_once语句:

    引用文件路径,路径必需正确。   eg:include_once("smarty/Smarty.class.php");

    2 $smarty=new Smarty();

    新建一个对象smarty, 实例化一个对象。

    3 $smarty->template_dir=“”;

    指定$smarty对象使用的tpl模板的路径,它是一个目录,默认目录为当前的templates的目录,实际编程中,可能要指定目录。

    4 $smarty->compile_dir=””;

    指定$smarty对象的编译时的目录,就是smarty编译模板的目录,linux服务器,请你确认有可写可读权限。通常chmod -R 777 filename 修改权限,默认情况下它编译目录是当前的目录下的templates_c。

    5 $smarty->left_delimiter 与 $smarty->right_delimiter;

    查找模板变量左右的分割符,默认情况下为{ } 为了与script中括号相区别,通常写为<{ }>.

    6 $tp1->cache_dir=”./”;

    模板文件缓存的位置,Smarty最大的优点在于可以缓存,这里设置缓存的目录,默认情况下当前目录下的cache目录,同上,linux确保它的可读可写性。

    7 $smarty->cache-lifetime=60*60*24;

    这里以秒为单位计算缓存有效的时间,第一次缓存时间到期时Smarty的caching变量设置为true时缓存将被重建。-1表示建立缓存从不过期,为0时表示每次程度执行时缓存被重新建立,上述一天。

    8 $smarty->catching=true;

    缓 存方式三个状态。0:Smarty'默认值,表示不对模板进行缓存。1:Smarty使用cache_lifetime来决定是否结束。2:表示 Smarty将使用cache被建立时使用cache_lifetime这个值,习惯上用true和false来表示是否进行缓存。

    Smarty常用语法

    1.foreach : 循环简单数组,它是一个选择性的section循环.

    form指定循环的数组变量,item循环变量的名称,循环次数由from指定数组变量的个数决定。

    数组为空,执行<{foreachelse}>语句.格式如下:

    <{foreach from=$newsArray item=newID}>

         The Id:<{$newsID.newsID}>

         The Content:<{$newsID.newsTitle}>

    <{foreachelse}>

         No content to put

    <{/foreach}>

    2.section: 用于设计模板内的循环块。可完成foreach语句所完成所有的功能。Section的格式:

    <{section loop= $varName[,start=$start,step=$setp,max=$max,$show=true]}>

    name: section的名称,不用加$;

    $loop: 要循环的变量,程度中要使用assign对这个变量进行操作。

    $start: 开始循环的下标。默认为0;

    $step: 每次循环下标的增数;

    $show : boolean型。决定是否对于这块进行显示。默认为true;

    <{section}>的属性;

    index:循环下标。默认为0;

    index_prev:当前下标的上一个值,默认为-1;

    index_next:当前下标的下一个值,默认为1;

    first:是否为第一下循环;

    last:是否为最后一个循环;

    iteration:循环个数;

    rownum:当前行号,iteration的别名;

    loop:最后一个循环号。Section的循环次数;

    show:是否显示;

    <{section loop=$News}>

         新闻编号:<{$News[loop].newID}><br>

         新闻内容:<{$News[loop].newTitle}><br>

    <{sectionelse}>

         I am sorry

    <{/section}>

    对于一维数组:

    {section name=row loop=$list}

    {$list.name}

    {/section}

    对于二维数组:

    {section name=row loop=$list}

    {$list[row].name}

    {/section}

    另外还可以直接单一输出:  

       < { $commendList.0.infoType } >

       < { $commendList.1.infoType } >

    对于三维数组:

    <{section name=cate loop=$myarray}>

    <{section name=scate loop=$myarray[cate]}>

    id ------> <{$myarray[cate][scate].id}>

    title --------> <{$myarray[cate][scate].title}>

    url ----------> <{$myarray[cate][scate].url}>

    <{/section}>

    <{/section}>

    3.if用法:

    {if $list[row].name eq “1"}

    星期1

    {elseif $list[row].name==“2"}

    星期2

    {else}

    默认

    {/if}

    4.{ include file=a.template}

    <{include file=uhead.html}>

    5.Literal

    标签区域内的数据将被当作文本处理,此时模板将忽略其

    内部的所有字符信息. 该特性用于显示有可能包含大括号等字符信

    息的 javascript 脚本.

    <literal>

    <script>

    alert('js');

    </script>

    <literal>

    6.assign

    用于在模板被执行时为模板变量赋值.

    {assign var="name" value="Bob"}

    $smarty->assign("newsArray", $array);

    //编译并显示位于./templates下的index.tpl模板

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

    Smarty常用函数

    count_characters 计算变量里的字符数 {$articleTitle|count_characters}

    cat 将cat里的值连接到给定的变量后面. {$articleTitle|cat:" yesterday."}

    count_paragraphs计算变量里的段落数量。 {$articleTitle|count_paragraphs}

    count_sentences 计算变量里句子的数量。{$articleTitle|count_sentences}

    count_words 计算变量里的词数 。{$articleTitle|count_words}

    date_format格式化从函数strftime()获得的时间和日期。 {$yesterday|date_format:"%H:%M:%S"}

    default 为空变量设置一个默认值{$articleTitle|default:"no title"}

    escape 用于html等转码

    index.tpl:

    {$articleTitle}

    {$articleTitle|escape}

    {$articleTitle|escape:"html"} {* escapes & " ' < > *}

    {$articleTitle|escape:"htmlall"} {* escapes ALL html entities *}

    {$articleTitle|escape:"url"}

    {$articleTitle|escape:"quotes"}

    <a href="mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>

    OUTPUT:

    'Stiff Opposition Expected to Casketless Funeral Plan'

    &#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;

    &#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;

    &#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;

    %27Stiff+Opposition+Expected+to+Casketless+Funeral+Plan%27

    \'Stiff Opposition Expected to Casketless Funeral Plan\'

    <a href="mailto:%62%6f%62%40%6d%65%2e%6e%65%74">&#x62;&#x6f;&#x62;&#x40;&#x6d;&#x65;&#x2e;&#x6e;&#x65;&#x74;</a>

    indent 在每行缩进字符串,默认是4个字符{$articleTitle|indent:1:"\t"}

    lower 小写{$articleTitle|lower}

    nl2br

    regex_replace {$articleTitle|regex_replace:"/[\r\t\n]/":" "}

    replace {$articleTitle|replace:"被替换":"Vineyard"}

    spacify 每个字符间插入字符{$articleTitle|spacify:"^^"}

    string_format格式化字符串 {$number|string_format:"%.2f"}

    strip 用一个空格或一个给定字符替换所有重复空格,换行和制表符. {$articleTitle|strip:"&nbsp;"}

    strip_tags 去除<和>标签,包括在<和>之间的任何内容.{$articleTitle|strip_tags}

    truncate

    upper 大写

    wordwrap以指定段落的宽度,默认80.第二个参数,可以指定在约束点使用什么字符(默认是换行符\n).{$articleTitle|wordwrap:30:"\n":true}

    html_options

    <select name="media_id" id="media_id">

            <{html_options options=$media_id selected=$ap.media_id}>

       </select>

    html_radios

    <{html_radios name="is_valid" options=$is_valid checked=$ap.is_valid separator=" "}>

     

    转载:http://blog.163.com/023_dns/blog/static/11872736620097132555544/

     

    不平凡的意义在于坚持
  • 相关阅读:
    typeOf操作符及数据类型
    图片轮播 js
    百度地图API学习
    jq 使页脚固定在底部
    js 动态自动添加 删除
    background-size 兼容ie8以下浏览器的方法
    opacity css3 ie8以下兼容方法
    ie 6、7/position: absolute bug解决方法
    IE 下的rgba背景透明
    2017腾讯实习生春招前端初面总结
  • 原文地址:https://www.cnblogs.com/wluomo/p/4023613.html
Copyright © 2011-2022 走看看