簡體   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