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. chartControl 饼状图小Demo

    Short Description     The Pie Chart is represented by the Pie3DSeriesView object, which belongs to P ...

  2. Nutch2.3分布执行过程中Mongodb中数据的变化

    inject $ nutch inject /opt/nutch/runtime/local/urls/ > db.stats() { "db" : "nutch& ...

  3. SQLSERVER分布式事务使用实例

    实例一 尊重原著作:本文参考自http://www.jb51.net/article/43540.htm --BEGIN DISTRIBUTED TRANSACTION [transactionnam ...

  4. 关于Oracle备份中的fractured block

    One danger in making online backups is the possibility of inconsistent data within a block. For exam ...

  5. VS的Release模式配置技巧

    环境:如果要将bin\release目录作为我们产品的发布目录或者产品来源目录,那么release目录中就只能有产品需要用到的东西,但是以VS默认的配置编译的话会生成比较讨厌的.pdb,.vshost ...

  6. leetcode Binary Tree Right Side View python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  7. MAC上使用maven打android的包,报错:No Android SDK path could be found. 解决办法

    对android工程运行mvn compile出现如下信息: No Android SDK path could be found. You may configure it in the pom u ...

  8. Python进阶之闭包

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

  9. 文本域textarea显示输入剩余字数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. CreateFileMapping共享内存时添加Global的作用

    来源:http://www.cnblogs.com/elvislogs/articles/ShareMemory.html 通常使用CreateFileMapping建立共享内存时名称中没有加入&qu ...