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'

'Stiff Opposition Expected to Casketless Funeral Plan'

'Stiff Opposition Expected to Casketless Funeral Plan'

'Stiff Opposition Expected to Casketless Funeral Plan'

%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">bob@me.net</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/

smarty函数-转载的更多相关文章

  1. Smarty 函数

    html_checkboxes 自定义函数 html_checkboxes 根据给定的数据创建复选按钮组. 该函数可以指定哪些元素被选定. 要么必须指定 values 和 ouput 属性,要么指定 ...

  2. smarty模板配置代码详细说明及如何注册自己的smarty函数

    下面为smarty模板的配置文件,smarty配置的详细说明以及如何注册自己所需要的smarty函数 config.inc.php <?php /** * Smarty 调用 * www.daf ...

  3. C++析构函数定义为虚函数(转载)

    转载:http://blog.csdn.net/alane1986/article/details/6902233 析构函数执行时先调用派生类的析构函数,其次才调用基类的析构函数.如果析构函数不是虚函 ...

  4. smarty函数

    内置函数(重要的几个): <{html_checkboxes name='nation' values=$code output=$name selected=$selid separator= ...

  5. MySQL常用函数 转载

    一.数学函数ABS(x)                    返回x的绝对值BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制)CEILING(x)                返 ...

  6. Java中的Random()函数-----转载

    Java中的Random()函数 (2013-01-24 21:01:04) 转载▼ 标签: java random 随机函数 杂谈 分类: Java 今天在做Java练习的时候注意到了Java里面的 ...

  7. JavaScript 数组操作函数--转载+格式整理

    JavaScript 数组操作函数(部分)--转载+格式整理 今天看了一篇文章,主要讲的对常用的Js操作函数:push,pop,join,shift,unshift,slice,splice,conc ...

  8. MySql常用日期函数(转载)

    /*date_add(date,interval expr type)和date_sub(date,interval expr type)执行日期运算. date 是一个 datetime 或date ...

  9. mysql字符串函数(转载)

    对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL,返回NULL. mysq ...

随机推荐

  1. html5滑动手势

    <div id="divMove" style="height: 100px;"></div> <div id="sli ...

  2. Timeout expired 超时时间已到. 达到了最大池大小 错误及Max Pool Size设置

    参考数据库链接串: <add key="data" value="server=192.168.1.123; Port=3306; uid=root; pwd=ro ...

  3. 在node.js中使用ejs的demo 第五篇

    先说明一下我的项目的目录解构: 本项目中渲染的时候都是通过在index.js页面里面,来使用index.ejs的,首先引用必须的模块: var express = require('express') ...

  4. 安装jdk和tomcat

    安装jdk和tomcat 1,准备工作 虚拟机 VMware :liunx系统镜像 bebian :连接操作软件 putty: 开源图像FTP客户端winspc: Java 语言的软件开发工具包 JD ...

  5. Lua的元方法__newindex元方法

    上一篇介绍了__index元方法,总结来说:__index元方法是用于处理访问table中不存在的字段时的情况. 而今天,介绍的__newindex元方法,总结来说,就是:用于处理给table中不存在 ...

  6. Linux下为何都是文件的理解

    所谓“文件”,就是在我们的电脑中,以实现某种功能.或某个软件的部分功能为目的而定义的一个单位. Linux都是以文件的形式存在,当我们访问某个文件(Linux中的文件有目录,连接,普通文本),由于Li ...

  7. Python进阶之模块与包

    模块 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB","S ...

  8. 图片延时加载jquery.inview.js用法详解

    我们在网站上总能见到这样的效果,若是有图片,图片都是先用loading加载一小段时间,然后紧接着出来要显示的图片,即效果如下: v2_loading.gif,几秒钟时间过渡到v2_pic_01_s.j ...

  9. jitpack让使用第三方依赖库更简单

    在开发过程中,使用第三方优秀依赖库是个很常见的问题,有的时候是maven,或者gradle, 或者sbt,大部分库工程,都会有对应的gradle,maven依赖代码,但是有的没有,尤其是使用的snap ...

  10. [原创]obj-c编程15[Cocoa实例02]:KVC和KVO的实际运用

    原文链接:obj-c编程15[Cocoa实例02]:KVC和KVO的实际运用 我们在第16和第17篇中分别介绍了obj-c的KVC与KVO特性,当时举的例子比较fun,太抽象,貌似和实际不沾边哦.那么 ...