zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    第六周上机任务
    java第四次作业
    第二次上机练习
    第三周作业
    第一次上机作业
    计算机1802刘思源
    第五次上机作业
    第六周作业
    第四次上机作业
    第三次上机作业
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/3524882.html
Copyright © 2011-2022 走看看