Get Smarty

Donate

Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

テンプレート関数プラグイン

void smarty_function_name( $params,  
  $template);  
array $params;
object $template;
 

テンプレートからテンプレート関数に渡された全ての 属性 は、 連想配列として $params に格納されます。

関数の出力(戻り値)はテンプレート関数のタグの部分と置き換えられます(例: {fetch} 関数)。 あるいは何も出力せずに単に他のタスクを実行する事ができます(例: {assign} 関数)。

関数によっていくつかの変数をテンプレートに割り当てる必要がある、 もしくは Smarty に提供された他の機能を使う必要がある場合は、 提供された $template オブジェクトを使用して $template->foo() のようにします。

Example 16.1. 出力ありのテンプレート関数プラグイン


<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.eightball.php
 * Type:     function
 * Name:     eightball
 * Purpose:  ランダムに回答を出力する
 * -------------------------------------------------------------
 */
function smarty_function_eightball($params, $template)
{
    $answers = array('はい',
                     'いいえ',
                     'わかりません',
                     '可能性は低い',
                     '今は答えられません',
                     '実はもう実現しているのかも……');

    $result = array_rand($answers);
    return $answers[$result];
}
?>


次のようにテンプレートで使用する事ができます。

質問: 将来、タイムトラベルは実現可能でしょうか?
答え: {eightball}.
    

Example 16.2. 出力なしのテンプレート関数プラグイン


<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.assign.php
 * Type:     function
 * Name:     assign
 * Purpose:  テンプート変数に値を割り当てる
 * -------------------------------------------------------------
 */
function smarty_function_assign($params, $template)
{
    if (empty($params['var'])) {
        trigger_error("assign: パラメータ 'var' がありません");
        return;
    }

    if (!in_array('value', array_keys($params))) {
        trigger_error("assign: パラメータ 'value' がありません");
        return;
    }

    $template->assign($params['var'], $params['value']);     
    
}
?>

      


registerPlugin() および unregisterPlugin() も参照ください。

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors