Smarty 模板显示两次函数结果

The Smarty template shows two time the function result

我有一个聪明的项目:

查看 test.php:

<?php

require($_SERVER['DOCUMENT_ROOT'] . '/smartyHeader.php');

function test1($args){
    $str="test1";

    return $str;
}

$smarty->registerPlugin('block' ,'hsp', 'test1');

$smarty->display('php/test.tpl');

test.tpl:

<html>
    <head>
        <title> Ho </title>
    </head>
    <body>

        {hsp times="1"}{/hsp}

    </body>
</html>

你看我只调用了一次hsp函数。

但浏览器显示两次结果:


edit-01

smartyHeader.php 只是创建 smarty 实例:

<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/libs/Smarty.class.php');

$smarty = new Smarty();
$smarty->caching = true;
$smarty->cache_lifetime = 120;
$smarty->template_dir = $_SERVER['DOCUMENT_ROOT'].'/templates';
$smarty->compile_dir = $_SERVER['DOCUMENT_ROOT'].'/templates_c';

在你的函数上试试这个变体

function test1($params, $content)
{
  if( $content !== null )
  {
    $str="test1";
    return $str;
  }
}

function test1($params, $content, &$smarty, &$repeat)
{
  if( $repeat === false )
  {
    $str="test1";
    return $str;
  }
}

你的自定义函数被 smarty 调用了两次,一次在块的开头,一次在块的结束标记处,你可以测试 $content === null(必须明确为 null)或 $repeat === false.

https://www.smarty.net/docsv2/en/plugins.block.functions.tpl

By default your function implementation is called twice by Smarty: once for the opening tag, and once for the closing tag. (See $repeat below on how to change this.)

The value of the $content variable depends on whether your function is called for the opening or closing tag. In case of the opening tag, it will be NULL, and in case of the closing tag it will be the contents of the template block. Note that the template block will have already been processed by Smarty, so all you will receive is the template output, not the template source.

The parameter $repeat is passed by reference to the function implementation and provides a possibility for it to control how many times the block is displayed. By default $repeat is TRUE at the first call of the block-function (the opening tag) and FALSE on all subsequent calls to the block function (the block's closing tag). Each time the function implementation returns with $repeat being TRUE, the contents between {func}...{/func} are evaluated and the function implementation is called again with the new block contents in the parameter $content.