smarty block_function

什么是block function

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

{block}...{/block}
  • 1
  • 1

block function的形式同上面类似。

{func}...{/func}
  • 1
  • 1

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

如何定义block function

smarty_block_name($params, $content, $template, &$repeat);
  • 1
  • 1

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

结合例子的进一步说明

example 1

block.my_test.PHP

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}";
    }   
}   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

tpl.test.htm

{my_test name="ball" gender="male"}
this is my test
{/my_test}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

页面输出

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
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

本例说明 
1. my_test的确被调用两次 
2. {my_test}(标签开始)处的调用,对应$repeat = true, 且此时$content值为空。{/my_test}(标签结束)处的调用,对应$repeat = false, 此时$content的值为block块中的内容(this is my test)。 
3. block的属性(name, gender)可以通过$parmas取到,且在任何位置(标签的开始和结束)都可以取到。 
4. echo和return的值,都将在页面显示。

example 2 – 对$repeat的说明

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

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>";
    }   
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

tpl.test.htm

{my_test count=3}
this is my test
{/my_test}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

页面输出

this is my test --1
this is my test --2
this is my test --3
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

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

example 3 – 实际应用

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

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

tpl中的代码可能是这样的

{html_link href="http://www.sogou.com" color="green"}
this is a real case
{/html_link}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

页面中得到的html源码如下

<a href='http://www.sogou.com' style='color:green' >
this is a test
</a>
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

如何让你的block function生效

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

 
 
原文地址:https://www.cnblogs.com/KLYY/p/6767697.html