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>
复制代码

 

 

 

 

 

posted on   荣锋亮  阅读(300)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示