如何将 smarty 变量传递给 php 函数?

Geo*_*oub 3 html php smarty

PHP代码:

function show_playlist_form($array)
{
    global $cbvid;
    assign('params',$array);

    $playlists = $cbvid->action->get_channel_playlists($array);
    assign('playlists',$playlists);

    Template('blocks/playlist_form.html');
}
Run Code Online (Sandbox Code Playgroud)

HTML 代码(内部智能):

<html><head></head>
<body>
{show_playlist_form}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

这一切都可以在剪辑桶视频脚本中找到。html 代码调用 php 函数,其中显示playlist_form.html。但是,我有兴趣在 smarty 定义的标签show_playlist_form中添加一个整数值,以便将其传递给 php show_playlist_form($array)中的函数,然后该函数将整数分配给$array

我尝试过,假设我有兴趣传递整数1

{show_playlist_form(1)} 
Run Code Online (Sandbox Code Playgroud)

致命错误: Smarty错误:[在/home/george/public_html/styles/george/layout/view_channel.html第4行]:语法错误:无法识别的标签:show_playlist_form(1)(Template_Compiler.class.php,第447行)在/ home/george/public_html/includes/templatelib/Template.class.php1095行

{show_playlist_form array='1'}
Run Code Online (Sandbox Code Playgroud)

html 代码有效,但我什么也没得到(空白)。

那么,它不起作用,我能做什么?我需要将整数值传递给函数。

IMS*_*SoP 5

您在这里寻找的是实现一个接收参数的“自定义模板函数”。

如函数插件文档所示,您创建的函数将接收两个参数:

  • 来自 Smarty 标签的命名参数的关联数组
  • 代表当前模板的对象(例如用于分配附加的 Smarty 变量)

例如,如果您这样定义:

function test_smarty_function($params, $smarty) {
      return $params['some_parameter'], ' and ', $params['another_parameter'];
}
Run Code Online (Sandbox Code Playgroud)

test并以如下名称向 Smarty 注册:

$template->registerPlugin('function', 'test', 'test_smarty_function');
Run Code Online (Sandbox Code Playgroud)

然后你可以在你的模板中使用它,如下所示:

{test some_parameter=hello another_parameter=goodbye}
Run Code Online (Sandbox Code Playgroud)

哪个应该输出这个:

hello and goodbye
Run Code Online (Sandbox Code Playgroud)

就您而言,您可能想要这样的东西:

function show_playlist_form($params, $smarty) {
     $playlist_id = $params['id'];
     // do stuff...
}
Run Code Online (Sandbox Code Playgroud)

和这个:

{show_playlist_form id=42}
Run Code Online (Sandbox Code Playgroud)