繁体   English   中英

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

[英]How can I pass a smarty variable to a php function?

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');
}

HTML代码(内部很脏):

<html><head></head>
<body>
{show_playlist_form}
</body>
</html>

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

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

{show_playlist_form(1)} 

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

{show_playlist_form array='1'}

的HTML代码工作,但我什么也没有(空白)。

所以,它没有用,我该怎么办? 我需要将整数值传递给函数。

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

有关功能插件的文档所示,您创建的函数将收到两个参数:

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

因此,例如,如果您定义以下内容:

function test_smarty_function($params, $smarty) {
      return $params['some_parameter'], ' and ', $params['another_parameter'];
}

并使用以下名称test将其注册到Smarty:

$template->registerPlugin('function', 'test', 'test_smarty_function');

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

{test some_parameter=hello another_parameter=goodbye}

应该输出以下内容:

hello and goodbye

在您的情况下,您可能想要这样的东西:

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

和这个:

{show_playlist_form id=42}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM