donglugou6652
2014-07-23 22:03
浏览 49
已采纳

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

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>

This all can be found in clip-bucket video script. The html code calls the php function, where it shows playlist_form.html. However, I'm interested in adding an integer value in the smarty defined tag show_playlist_form so that it would pass it to the function in php show_playlist_form($array) and then the function would assign the integer into $array.

I tried, assuming I'm interested in passing the integer 1:

{show_playlist_form(1)} 

Fatal error: Smarty error: [in /home/george/public_html/styles/george/layout/view_channel.html line 4]: syntax error: unrecognized tag: show_playlist_form(1) (Template_Compiler.class.php, line 447) in /home/george/public_html/includes/templatelib/Template.class.php on line 1095

{show_playlist_form array='1'}

The html code worked but it I got nothing (blank).

So, it didn't work, what can I do? I need to pass integer values to the function.

图片转代码服务由CSDN问答提供 功能建议

PHP代码:

   function show_playlist_form($ array)
 {
 global $ cbvid; 
 assign('params',$ array); 
 
 $ playlists = $ cbvid-&gt; action-&gt; get_channel_playlists($ array); \  n assign('播放列表',$播放列表); 
 
模板('blocks / playlist_form.html'); 
} 
   
 
 

HTML 代码(SMARTY INSIDE):

 &lt; html&gt;&lt; head&gt;&lt; / head&gt; 
&lt; body&gt; 
 {show_playlist_form} 
&lt;  / body&gt; 
&lt; / html&gt; 
   
 
 

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

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

  {show_playlist_form(1)} 
   
 
 

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

  {show_playlist_form array ='1'} 
   
 
 

html代码工作但我什么都没有(空白)。

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douchi2022 2014-07-23 22:27
    已采纳

    What you are looking for here is to implement a "custom template function" which receives parameters.

    As shown in the documentation on function plugins, the function you create will receive two parameters:

    • an associative array of named parameters from the Smarty tag
    • an object representing the current template (useful for e.g. assigning additional Smarty variables)

    So for instance, if you define this:

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

    And register it with Smarty under the name test like this:

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

    Then you could use it in your templates like this:

    {test some_parameter=hello another_parameter=goodbye}
    

    Which should output this:

    hello and goodbye
    

    In your case, you probably want something like this:

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

    and this:

    {show_playlist_form id=42}
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题