Smarty入门--内建函数
来源:互联网 发布:手机视频缓存软件 编辑:程序博客网 时间:2024/06/14 15:11
Smarty入门–内建函数
Smarty模板中自带了一些内建函数,用户不能创建和内建函数同名的函数。
if
这个太常用来了。判断
衍生的有:else
和elseif
使用以下条件修饰词: eq、ne、neq、gt、lt、lte、le、gte、ge、is even、is odd、is not even、is not odd、not、mod、div by、even by、odd by、==、!=、>、<、<=、>=
例子:
{if $a == 1}{elseif $a == 2}{else}{/if}
foreach
循环啊,算是smarty中常用的函数了。
具体形式根据传入的函数来写,可以嵌套。
foreach还有一些参数:
例
{*具体形式根据传入的函数来写,可以嵌套。*}{foreach name=a item=contact from=$contacts} {*判断是不是第一次循环*} {if $smarty.foreach.a.first} {*不显示该条循环*} {$smarty.foreach.a.show=false} {/if} {foreach name=b key=key item=item from=$contact} {$key}: {$item}<br> {*显示循环outer的次数*} {$smarty.foreach.a.iteration} {*显示循环b的次数*} {$smarty.foreach.b.iteration} {foreachelse} {$key}:"不存在!!" {/foreach}{/foreach}{*显示a的循环次数*}{$smarty.foreach.a.total}
section
用于遍历数组
详细:
start:循环执行的初始位置. 如果该值为负数,开始位置从数组的尾部算起.例如:如果数组中有7个元素,指定start为-2,那么指向当前数组的索引为5.非法值(超过了循环数组的下限)将被自动调整为最接近的合法值.
step:该值决定循环的步长. 例如指定step=2将只遍历下标为0、2、4等的元素.如果step为负值,那么遍历数组的时候从后向前遍历.
还有sectionelse这么个东西,和foreachelse一样就是,值为空的时候就运行。
还有这么多参数
例子:
{section name=student loop=$student_array}<h1>ID:{iteration}</h1><h1>name:{$student['name']}</h1><h1>age:{$student['age']}</h1><h1>name:{$student['name']}</h1>{sectionelse}<h1>ID:{iteration}</h1><h1>name:NULL</h1><h1>age:NULL</h1><h1>name:NULL</h1>{/section}
和foreach类似。
include
标签用于在当前模板中包含其它模板. 当前模板中的变量在被包含的模板中可用.
算是比较常用。
{include file="header.tpl" title="ILoveSmarty" auther="Jiang"}{*body.tpl中的内容不会输出会保存在data中*}{include file="body.tpl" assign="data"}{*输出信息*}{$data}
include_php
inluce_php 函数用于在模板中包含 php 脚本.如果设置了安全模式,被包含的脚本必须位于
assign 需要说一下,他定义的会获取引入app的所有输出,
但是,注意!!这里是存成一个字符串。
下面有例子。
例子:
test.php
$this->assign('name',"Tom");$this->assign('age',"24");echo "Hi";var_dump("Hellow");
tpl
{*引入php*}{include_php file="./test.php" assign="data"}<h1>{$name}</h1><h1>{$age}</h1>{*输出data信息*}<h1>{$data}</h1>
insert
Insert函数类似欲include函数,不同之处是 insert所包含的内容不会被缓存,每次调用该模板都会重新执行该函数.
这东西是用来调用方法的,调用一个已经存在的方法,用来生成一些东西。
可以用来引入广告,天气预报之类的实时更新的东西。
name,调用方法的名字,实际上是调用方法的名字的一部分。
按照规范,名字应该是insert_name(),比如说name=heiheihei,函数名就是insert_heiheihei()
assign的效果和上面的一样存住输出。
script,这个就是在调用包含方法之前包含指定的php脚本,说白了这个是引入,name是调用。
[var …]参数,一可以有多个,以数组形式传入方法,下面例子看。
例子:
可以是这样的。
test.php (这个是包含被调用方法的php文件。)
<?php//这是要被调用的方法function insert_eat(array $foods){ foreach($foods as $name => $food) { echo $name . "吃" . $food . "!!!\n"; }}?>
tpl
{*调用insert_eat()方法,参数为array("Tom"=>"banana","Jerry"=>"apple"),在调用方法前读取test.php文件,方法的输出保存在data中*}{insert name="eat" assign="data" script="./test.php" Tom="banana" Jerry="apple"}{*输出信息*}<h1>{$data}</h1>
注意:
1.例子出现了一个很尴尬的问题,在读参数的时候,name=eat assign=data
也被当做参数读了进来,不知道是个别现象还是都这样,使用时要注意。
2.我的Smarty环境要求啊我起名用如下格式:smarty_insert_eat
比手册里写的多了个smarty_
不知道是我的环境问题还是设置问题。
3.assign=data
不起作用。
太乱了。。。。还是别用了。。。。。
literal
Literal 标签区域内的数据将被当作文本处理,此时模板将忽略其内部的所有字符信息. 该特性用于显示有可能包含大括号等字符信息的 javascript 脚本. 当这些信息处于 {literal}{/literal} 标签中时,模板引擎将不分析它们,而直接显示.
这个我还真没用过,不过倒是经常看见。因为现在做的项目的开发规范不允许我们直接在tpl里写js脚本。
例子:
{*literal标签内的信息不会被Smarty识别*}{literal}function isblank(field) { if (field.value == '') { return false; } else { document.loginform.submit(); return true; }}{/literal}
config_load
该函数用于从配置文件中加载变量.
基本上用不到。。。。
scope的作用域分为
local说明该变量的作用域为当前模板.
parent 说明该变量的作用域为当前模板和当前模板的父模板(调用当前模板的模板). global 说明该变量的作用域为所有模板.
global属性可以被scope覆盖。
例:
{config_load file="colors.conf"}<html><title>颜色</title><body bgcolor="{#bodyBgColor#}"><table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}"> <tr bgcolor="{#rowBgColor#}"> <td>First</td> <td>Last</td> <td>Address</td> </tr></table></body></html>
配置文件:colors.conf
# global variablespageTitle = "Main Menu"bodyBgColor = #000000tableBgColor = #000000rowBgColor = #00ff00[Customer]pageTitle = "Customer Info"[Login]pageTitle = "Login"focus = "username"Intro = """This is a value that spans more than one line. you must enclose it in triple quotes."""# hidden section[.Database]host=my.domain.comdb=ADDRESSBOOKuser=php-userpass=foobar
smarty都没什么人用了,更别说这个方法了。
capture
捕获。
capture
用来捕获数据储存到一个变量里。
{capture name=abc}
…{/capture}
之间的信息会被捕获。
如果没有捕获到值则为空。
这里要注意,capture内全部值都会被捕获为值,包括smarty变量和HTML代码。
{*开始捕获数据*}{capture name=abc}{*读取数据*}{include file="text1.tpl"}{/capture}{*如果abc捕获到了值,输出。*}{if $smarty.capture.abc ne ""}{$smarty.capture.abc}{/if}
例子:
{*开始捕获数据*}{capture name=abc}{*读取数据*}{*name的值为"Tom"*}<h1>name:{$name}</h1>{/capture}{*如果abc捕获到了值,输出。*}{if $smarty.capture.abc ne ""}{$smarty.capture.abc}{/if}
结果:
ldelim,rdelim
用来代替输出{},不是很理解为什么属于内建函数。
例子:
<h1>I Love {ldelim}Smarty{rdelim}!</h1>
这是输出:
I Love {Smarty}!
php
就是在标签中可以直接使用php代码。
例子:
{php}include("test.php");{/php}
strip
移除标签内的首位空格和所有回车
例子:
{strip}<table border=0> <tr> <td> <A HREF="{$url}"> <font color="red">This is a test</font> </A> </td> </tr></table>{/strip}
输出:
<table border=0><tr><td><A HREF="http://my.domain.com"><font color="red">This is a test</font></A></td></tr></table>
- Smarty入门--内建函数
- smarty内建函数
- smarty 内建函数
- smarty内建函数
- smarty内建函数
- smarty内建函数
- SMARTY内建函数
- smarty内建函数
- smarty内建函数
- smarty内建函数
- smarty内建函数
- smarty内建函数
- smarty内建函数
- smarty内建函数
- smarty内建函数
- Smarty内建函数
- Smarty内建函数
- smarty的内建函数
- Hexo更换主题
- iptables——实战防火
- tableViewCell删除按钮自定义
- 属性文件操作类PropertiesReader
- facebook开源预测工具Prophet安装方法
- Smarty入门--内建函数
- 1007
- 兰顿蚂蚁
- 12.CentOS7下PostgreSQL安装过程
- Java垃圾回收机制
- Ajax
- linux shell编程 HelloWorld
- 程序员到高级程序员,只需要10个步骤!
- 关于linux yum remove使用方法与处理措施