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()函数的执行.如果一个函数被加进缓存,那么实际输出的内容将用缓存来代替. 缓存可让事物非常快速的执行,特别是带有长计算时间的模板.一旦 ...
随机推荐
- docker添加国内仓库安装iredmail
centos 7: 1.yum install docker or yum update docker sudo tee /etc/docker/daemon.json <<-'EOF'{ ...
- oaf 动态创建table vo (转)
原文地址:如何动态创建table 需求: 因为系统中有几千个QA plan 但是不能手动创建几千个 质量收集页面所有需要根据 不同的plan 动态创建对应的 质量收集页面. 但是创建tabel 都要绑 ...
- 循环大法——一次性理清forEach/for-in/for/$each
国寿的这个项目写得我基础都忘完了 近期会把vue和基础都并行复习.学习 forEach 适用于调用数组的每个元素,并将元素传递给回调函数,但是空数组是不会执行回调函数的.forEach适用于集合中的对 ...
- 利用padding-top/padding-bottom百分比,进行占位和高度自适应
在css里面,padding-top,padding-bottom,margin-top,margin-bottom取值为百分比的时候,参照的是父元素的宽度. 比如:父元素宽度是100px, 子元素p ...
- artDialog 弹窗提示
artDialog 弹窗提示,方便调用,不用去查文档了. /// <reference path="../../Scripts/artDialog5.0/artDialog.min.j ...
- bzoj1084&&洛谷2331[SCOI2005]最大子矩阵
题解: 分类讨论 当m=1的时候,很简单的dp,这里就不再复述了 当m=2的时候,设dp[i][j][k]表示有k个子矩阵,第一列有i个,第二列有j个 然后枚举一下当前子矩阵,状态转移 代码: #in ...
- noip2007-4
首先预处理f[i][j]表示i到j的路径 然后枚举i,j,如果f[i][j]<=s,那么 寻找最大的k,计算路径距离 计算最短的 代码: #include<bits/stdc++.h> ...
- DevExpress v17.2新版亮点——VCL篇(二)
用户界面套包DevExpress v17.2日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress VCL v17.2 的新功能,快来下载试用新版本! DPI ...
- beta阶段贡献分配实施
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2281] 要求1 每位组员的贡献分值 刘莹莹 王玉潘 潘世维 周昊 赵美增 ...
- Jmeter实现MySQL的增删改查操作
环境: JDBC驱动:mysql-connector-java-5.1.7-bin Jmeter:Jmeter3.0 1.导入JDBC驱动:测试计划-->浏览-->选择mysql-conn ...