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学习——内建函数 部分的更多相关文章

  1. smarty学习——内建函数(部分接上)

    9.{foreach} {foreachelse} 格式如下: {foreach $arrayvar as $itemvar} {foreach $arrayvar as $keyvar=>$i ...

  2. Smarty学习笔记(一)

    1.Smarty的配置: 将lib的内容复制到自己的工程,然后引入 实例化和配置Smarty基本属性: $smarty = new Smarty(); $smarty->left_delimit ...

  3. smarty学习——基本概念

    学习一种框架,我们最基本的就是掌握框架的思想,同时了解框架的基本语法. 1.对于定界符的了解 有的smarty模板标签都被加上了定界符. 默认情况下是 { 和},但它们是可被改变的.例如,我们假定你在 ...

  4. MVC架构学习之Smarty学习——病来而蔫

    前两天是五一小长假,而每次假期都想着如何如何刻苦一番,往往是自作多情.. 当然这次是有小病在身,多个借口吧. 一有病就蔫的不行...要锻炼了啊,脚估计也差不多了,游泳试试吧这周. 这次学习Smarty ...

  5. smarty 学习记录

    smarty模版是比较大众化的一个模版,在php开发过程当中被很多开发者视为最友好的模版之一,学习smarty课程对于很多培训机构来说也是列入了培训课程之一,那么很多方面就需要我们学习了一. 安装首先 ...

  6. smarty学习——编写扩展

    在进行了以上的开发环境的配置之后就是,进行进一步的学习,为了开发的方便我们一般会使用oop的编程思想,进行方便的处理 如下: 1.smartyUser 的创建 <?php require_onc ...

  7. Smarty学习笔记(二)

    1.引用 {include file="xxx.xxx" sitename="xxx"} 向引入的文件传入变量: {include file="xxx ...

  8. smarty学习——高级知识

    1.Objects 对象 smarty允许通过模板访问PHP对象.有两种方式来访问它们.一种是注册对象到模板,然后通过类似于用户自定义函数的形式来访问它. 另一种方法给模板分配对象,然后通过访问其它赋 ...

  9. smarty学习——缓存

    存被用来保存一个文档的输出从而加速display()或fetch()函数的执行.如果一个函数被加进缓存,那么实际输出的内容将用缓存来代替. 缓存可让事物非常快速的执行,特别是带有长计算时间的模板.一旦 ...

随机推荐

  1. Android 列表使用(ListView GridView Gallery图片计时滚动)

    ListView 作用: 1.将数据填充到布局. 2.处理用户的选择点击等操作. 根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAd ...

  2. Leetcode 22

    //这题感觉不如前两题回溯清楚,还要再看看class Solution { public: vector<string> generateParenthesis(int n) { vect ...

  3. 51. N-Queens 52. N-Queens II *HARD*

    1. 求所有解 The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two ...

  4. SSH执行远程命令和传送数据

    $ ssh user@host 'mkdir -p .ssh && cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub ...

  5. 简话Angular 01 初识Angular 数据绑定

    1. Angular有哪些突出优点 1) MVC 基于Html-Javascript 2) 依赖注入 3) 数据双向绑定,响应式页面设计 4) 模块化,自定义指令 2. 简话数据绑定 1) 代码: & ...

  6. vm options设置

    -Dfile.encoding=UTF-8 vmopiton.properties  加入 8A7674

  7. photoshop cc 智能切图

    这节分享一个photoshop cc 开始有的自动生成图标的方法 psd练习文件 http://pan.baidu.com/s/1pL2dwL1 1 工具:我这里用的是photoshop cc 201 ...

  8. 快速切题 sgu102.Coprimes 欧拉函数 模板程度 难度:0

    102. Coprimes time limit per test: 0.25 sec. memory limit per test: 4096 KB For given integer N (1&l ...

  9. oracle_sql语句的大全

    # 建立数据库struts   CREATE DATABASE IF NOT EXISTS struts DEFAULT CHARACTER SET GBK; 1.登录:(管理员)conn sys/p ...

  10. 深入理解java异常【绝对经典,推荐最少看五遍】

    http://blog.csdn.net/hguisu/article/details/6155636 补充:检查异常(checkedException)与运行异常,什么时候throw,什么时候thr ...