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. JS函数作用域及作用域链理解

    从事web开发工作,尤其主要是做服务器端开发的,难免会对客户端语言JavaScript一些概念有些似懂非懂的,甚至仅停留在实现功能的层面上,接下来的文章,是记录我对JavaScript的一些概念的理解 ...

  2. mvc 设置默认页技巧

    打开网址:http://xxxx.com自动跳转==>http://xxx.com/home/index 设置route入口: routes.MapRoute( name: "Main ...

  3. C# DataTable 详解

    添加引用 using System.Data; 创建表 //创建一个空表 DataTable dt = new DataTable(); //创建一个名为"Table_New"的空 ...

  4. 无法下载图片 App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file

    刚学线程通信,提示: 2016-01-27 11:11:02.246 20-9 gcd3 communicationOfThread[5193:298643] App Transport Securi ...

  5. 从C++strStr到字符串匹配算法

    字符串的匹配先定义两个名词:模式串和文本串.我们的任务就是在文本串中找到模式串第一次出现的位置,如果找到就返回位置的下标,如果没有找到返回-1.其实这就是C++语言里面的一个函数: extern ch ...

  6. 加密传输SSL协议2_传统加密

    原本到了睡觉的时间,但是做完了SSL的笔记还有GCC和Oracle等的好多的笔记,所以刻苦一点. The Priciple of Encryption/Decryption --conventiona ...

  7. 纯CSS 贴底部的布局(兼容各个浏览器包括IE6)

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

  8. Android API在不同版本系统上的兼容性

    随着安卓版本的不断更新,新的API不断涌出,有时候高版本的API会在低版本crash的. 如果minSdkVersion设置过低,在build的时候,就会报错(Call requires API le ...

  9. android sdk 更新问题——截止2014年6月10日有效

    因为墙的原因,很多人的sdk都更新不了,下面记录了我刚刚实现更新的方法: 进到Android SDK Manager,菜单Tools->Options..,这时弹出一个框,在这个框的下面Othe ...

  10. [LeetCode]题解(python):148-Sort List

    题目来源: https://leetcode.com/problems/sort-list/ 题意分析: 用nlog(n)的时间复杂度实现一个链表的排序. 题目思路: 用归并排序的思想,将链表用快慢指 ...