smarty学习——内建函数 部分
Smarty自带一些内建函数. 内建函数是模板语言的一部分. 用户不能创建名称和内建函数一样的自定义函数,也不能修改内建函数.
一.包含的内建函数
{$var=...}{append}{assign}{block}{call}{capture}{config_load}{debug}{extends}{for}{foreach},{foreachelse}
@index
@iteration
@first
@last
@show
@total
{break}
{continue}
{function}{if},{elseif},{else}{include}{include_php}{insert}{ldelim},{rdelim}{literal}{nocache}{php}{section},{sectionelse}
.index
.index_prev
.index_next
.iteration
.first
.last
.rownum
.loop
.show
.total
{setfilter}{strip}{while}
1.变量赋值函数
简单赋值:
{$userdalong='dalong'}
使用函数:
{$first=5}
{$second=6}
{$Addresult=$first+$second}
Addresult:{$Addresult}
赋值对象对象或者数组
{$user.name="Bob"}
user.name={$user.name}
2.append
是对于已经创建的模板变量进行穿件添加:
{append var='name' value='Bob' index='first'}
{append var='name' value='Meyer' index='last'}
The first name is {$name.first}.<br>
The last name is {$name.last}.<br>
3.assign 用于添加变量
{assign var="dalong" value="Smarty dalong demo" scope="global"}
可以使用php 进行访问
代码如下:
<?php
require_once 'smartyUser.php'; $
user=new smartyUser();
$user->fetch('conf.tpl'); // 没有这句不会输入任何信息
echo $user->getTemplateVars('dalong');
?>
结果输出:
Smarty dalong demo
同时我们也可以进行动态的修改
<?php
require_once 'smartyUser.php';
$user = new smartyUser ();
$user->fetch ( 'conf.tpl' );
echo $user->getTemplateVars ( 'dalong' );
echo '<br>';
$user->assign ( 'dalong', 'we change the default value' );
echo $user->getTemplateVars ( 'dalong' );
?>
同上 输出结果如下:
Smarty dalong demo
we change the default value
4. block 进行模块化显示
如下:
parent.tpl
<html>
<head>
<title>{block name="title"}Default Title{/block}</title>
<title>{block "title"}Default Title{/block}</title> {* short-hand *}
</head>
</html>
child.tpl
{extends file="parent.tpl"}
{block name="title"}
Page Title
{/block}
php
<?php
require_once 'smartyUser.php';
$user = new smartyUser ();
$user->display('child.tpl');
?>
输出:
<html>
<head>
<title>Page Title</title>
</head>
</html>
5. call 进行模板函数的调用
模板func 代码如下:
{* define the function *}
{function name=menu level=0}
<ul class="level{$level}">
{foreach $data as $entry}
{if is_array($entry)}
<li>{$entry@key}</li>
{call name=menu data=$entry level=$level+1}
{else}
<li>{$entry}</li>
{/if}
{/foreach}
</ul>
{/function}
{* create an array to demonstrate *}
{$menu = ['item1','item2','item3' => ['item3-1','item3-2','item3-3' => ['item3-3-1','item3-3-2']],'item4']}
{* run the array through the function *}
{call name=menu data=$menu} {call menu data=$menu} {* short-hand *}
php :
<?php
require_once 'smartyUser.php';
$user = new smartyUser ();
$user->display('func.tpl');
?>
显示结果:

6.capture
capture函数的作用是捕获模板输出的数据并将其存储到一个变量里,而不是把它们输出到页面.
任何在 {capture name="foo"}和{/capture}之间的数据将被存储到变量$foo中,该变量由name属性指定.
在模板中通过 $smarty.capture.foo 访问该变量. 如果没有指定 name 属性,函数默认将使用 "default" 作为参数. {capture}必须成对出现,
即以{/capture}作为结尾,该函数不能嵌套使用.
{* we don't want to print a div tag unless content is displayed *}
{capture name="banner"}
{capture "banner"} {* short-hand *}
{include file="get_banner.tpl"} {/capture}
{if $smarty.capture.banner ne ""}
<div id="banner">
{$smarty.capture.banner}
</div>
{/if}
7.extends
这种标签是在自模板中使用的,子模板是继承自已经存在的父模板。
8.for
进行循环操作
$smarty->assign('start',10);
$smarty->assign('to',5);
<ul> {for $foo=$start to $to}
<li>{$foo}</li> {forelse}
no iteration
{/for}
</ul>
smarty学习——内建函数 部分的更多相关文章
- smarty学习——内建函数(部分接上)
9.{foreach} {foreachelse} 格式如下: {foreach $arrayvar as $itemvar} {foreach $arrayvar as $keyvar=>$i ...
- Smarty学习笔记(一)
1.Smarty的配置: 将lib的内容复制到自己的工程,然后引入 实例化和配置Smarty基本属性: $smarty = new Smarty(); $smarty->left_delimit ...
- smarty学习——基本概念
学习一种框架,我们最基本的就是掌握框架的思想,同时了解框架的基本语法. 1.对于定界符的了解 有的smarty模板标签都被加上了定界符. 默认情况下是 { 和},但它们是可被改变的.例如,我们假定你在 ...
- MVC架构学习之Smarty学习——病来而蔫
前两天是五一小长假,而每次假期都想着如何如何刻苦一番,往往是自作多情.. 当然这次是有小病在身,多个借口吧. 一有病就蔫的不行...要锻炼了啊,脚估计也差不多了,游泳试试吧这周. 这次学习Smarty ...
- smarty 学习记录
smarty模版是比较大众化的一个模版,在php开发过程当中被很多开发者视为最友好的模版之一,学习smarty课程对于很多培训机构来说也是列入了培训课程之一,那么很多方面就需要我们学习了一. 安装首先 ...
- smarty学习——编写扩展
在进行了以上的开发环境的配置之后就是,进行进一步的学习,为了开发的方便我们一般会使用oop的编程思想,进行方便的处理 如下: 1.smartyUser 的创建 <?php require_onc ...
- Smarty学习笔记(二)
1.引用 {include file="xxx.xxx" sitename="xxx"} 向引入的文件传入变量: {include file="xxx ...
- smarty学习——高级知识
1.Objects 对象 smarty允许通过模板访问PHP对象.有两种方式来访问它们.一种是注册对象到模板,然后通过类似于用户自定义函数的形式来访问它. 另一种方法给模板分配对象,然后通过访问其它赋 ...
- smarty学习——缓存
存被用来保存一个文档的输出从而加速display()或fetch()函数的执行.如果一个函数被加进缓存,那么实际输出的内容将用缓存来代替. 缓存可让事物非常快速的执行,特别是带有长计算时间的模板.一旦 ...
随机推荐
- 原生js实现选项卡
html代码: <div class="tab"> <ul> <li class="selected">图片</li& ...
- Non-parseable POM 解决方法
两个build放在一起当然不行,把它们类似这样的合并起来.
- python-day38--IO模型
一. IO模型介绍 对于一个网络通信,IO涉及到两个阶段 1.操作系统等数据来 2.进程或线程等操作系统拷贝数据 记住这两点很重要,因为这些IO模型的区别就是在两个阶段上各有不同的情况. 二.阻塞IO ...
- zzuli1427 NO.6校赛----数字转换
1427: 数字转换 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 572 Solved: 153 SubmitStatusWeb Board Des ...
- OC MRC之set方法内存管理(代码分析)
// // main.m // 03-set方法的内存管理 // // Created by apple on 13-8-9. // Copyright (c) 2013年 itcast. All r ...
- sublime text 2 php 语法错误检查
使用sublime text 2 编写php程序的时候,保存代码的时候,直接检查出语法错误,有利于提高效率. 1.安装sublime text 2 package menu : preferences ...
- BZOJ3895 取石子
Orz PoPoQQQ 我等蒟蒻只能想到石子数 ≥ 2时的情况...1的时候就爆搜?大概是这个意思 最后再记忆化一下 /**************************************** ...
- 快速切题 sgu104. Little shop of flowers DP 难度:0
104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB PROBLEM Yo ...
- 微信小程序-隐藏和显示自定义的导航
微信小程序中不能直接操作window对象,document文档,跟html的树结构不相同. 实现类似导航的隐藏显示,如图效果: 点击网络显示或隐藏网络中包含的内容.其他类似. 如果是jquery很方便 ...
- Vue 框架中遇到的诀窍
问题一. 我需要渲染数组A,并根据 B数组中是否存在A中,给A添加 选中状态sel. 经过很焦虑的研究后,寻求帮助得到答案. 1.初始化数据时 A添加属性flag(bool)标识,如果B中存在A中某个 ...