首页 文章

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

提问于
浏览
1

PHP CODE:

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 CODE (SMARTY INSIDE):

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

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

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

{show_playlist_form(1)}

Fatal error: 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.php 在线 1095

{show_playlist_form array='1'}

HTML代码工作但我什么都没有(空白) .

所以,它不起作用,我该怎么办?我需要将整数值传递给函数 .

1 回答

  • 3

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

    the documentation on function plugins所示,您创建的函数将接收两个参数:

    • 来自Smarty标签的命名参数的关联数组

    • 表示当前模板的对象(对于例如分配其他Smarty变量很有用)


    例如,如果你定义这个:

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

    并将其注册为Smarty,名称为 test ,如下所示:

    $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}
    

相关问题