首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >smarty的block function(块函数)

smarty的block function(块函数)

作者头像
跑马溜溜的球
发布2020-12-07 15:35:25
发布2020-12-07 15:35:25
2.1K0
举报
文章被收录于专栏:日积月累1024日积月累1024

什么是block function

如果你使用过smarty3,一定对下面的代码不陌生。

代码语言:javascript
复制
{block}...{/block}

block function的形式同上面类似。

代码语言:javascript
复制
{func}...{/func}

它用标签圈起一个块,然后对这个块的内容进行操作。

如何定义block function

代码语言:javascript
复制
smarty_block_name($params, $content, $template, &$repeat);

这是手册中给出的定义,说明如下: 1. 默认你的函数将被Smarty调用两次,分别在开始标签位置和结束标签位置。 2. 函数将以以下方式输出内容到页面: * 直接echo * 对应结束标签位置的调用,函数的返回值会显示在页面。 * 从smarty3.1开始,开始标签位置的调用,函数的返回值也会显示在页面。 3. 模板中传递给块函数的属性都包含在params参数数组中 4. content的值,取决于函数在执行开始标签还是结束标签。当在开始标签时,它会是null; 当在结束标签时,它会是模板块里面全部的内容。 5. repeat是一个引用值,能控制块可以被显示多少次。当块函数被第一次调用时(开始标签),repeat默认是true; 随后的调用(结束标签)都是false。每将

结合例子的进一步说明

example 1

block.my_test.php

代码语言:javascript
复制
function smarty_block_my_test($params, $content, $smarty, &$repeat){
    if ($repeat){
        echo "start tag:<br>";
        var_dump($params);
        return "content: {$content}<br>";
    }

    if(!$repeat){
        echo "end tag:<br>";
        var_dump($params);
        return "content: {$content}";
    }   
}   

tpl.test.htm

代码语言:javascript
复制
{my_test name="ball" gender="male"}
this is my test
{/my_test}

页面输出

代码语言:javascript
复制
start tag:
array(2) { 'name' => string(4) "ball" 'gender' => string(4) "male" } content: 
end tag:
array(2) { 'name' => string(4) "ball" 'gender' => string(4) "male" } content: this is my test

本例说明 1. my_test的确被调用两次 2. {my_test}(标签开始)处的调用,对应repeat = true, 且此时content值为空。{/my_test}(标签结束)处的调用,对应repeat = false, 此时content的值为block块中的内容(this is my test)。 3. block的属性(name, gender)可以通过

example 2 – 对$repeat的说明

改写下smarty_block_my_test,实现这样一个功能:将{my_test}{/my_test}中的内容重复若干次,次数由属性值count指定。 block.my_test.php

代码语言:javascript
复制
function smarty_block_my_test($params, $content, $smarty, &$repeat){
    $count = $params['count'];
    static $num = 0;

    if(!$repeat){ 
        $num++;

        if ($num < $count ){
            $repeat = true;
        }

        return "{$content}--{$num}<br>";
    }   
}

tpl.test.htm

代码语言:javascript
复制
{my_test count=3}
this is my test
{/my_test}

页面输出

代码语言:javascript
复制
this is my test --1
this is my test --2
this is my test --3

这个例子着重说明了repeat的作用,只要repeat被设为true,{my_test}{/my_test}就会被再次调用

example 3 – 实际应用

一个实际中的例子是这样的:我们希望输出一个,可以根据参数决定的href, color, class等值。 block.html_link.php

代码语言:javascript
复制
function smarty_block_html_link($params, $content, $smarty, &$repeat){
    if(!$repeat){
        $href = $params['href'];

        $color = '';
        if(isset($params['color'])){
            $color = sprintf("style='color:%s'", $params['color']);
        }

        $class = '';
        if(isset($params['class'])){
            $class = sprintf("class='%s'", $params['class']);
        }

        $str = sprintf("<a href='%s' %s %s>%s</a>", $href, $color, $class, $content);

        return $str;
    }
}

tpl中的代码可能是这样的

代码语言:javascript
复制
{html_link href="http://www.sogou.com" color="green"}
this is a real case
{/html_link}

页面中得到的html源码如下

代码语言:javascript
复制
<a href='http://www.sogou.com' style='color:green' >
this is a test
</a>

如何让你的block function生效

有下面两种方式: 1. 使用registerPlungin() 具体可参见手册,有明确的注册block function的例子 2. 使用addPluginsDir(), 将插件(block.html_link.php)所在的目录告知smarty。此种方式要注意文件名和函数的命名。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/08/16 ,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 什么是block function
  • 如何定义block function
  • 结合例子的进一步说明
    • example 1
    • example 2 – 对$repeat的说明
    • example 3 – 实际应用
  • 如何让你的block function生效
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档