Get Smarty

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:

Check reviews of PHP Web Hosting, compatible with Smarty Templates

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

void smarty_function_name( $params,  
  &$smarty);  
array $params;
object &$smarty;
 

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

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

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

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


<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.eightball.php
 * Type:     function
 * Name:     eightball
 * Purpose:  ランダムに回答を出力する
 * -------------------------------------------------------------
 */
function smarty_function_eightball($params, &$smarty)
{
    $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, &$smarty)
{
    if (empty($params['var'])) {
        $smarty->trigger_error("assign: パラメータ 'var' がありません");
        return;
    }

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

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

      


register_function() および unregister_function() も参照してください。

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 16 plus 15? (Are you human?)

Sponsors [info]

Cheap Website Hosting @hostdone.com
Cheap Hosting @HostsVault
email marketing @ActiveCampaign
broadband deals @BroadbandGenie
Best UK Web Hosting @webhost.uk.net
Reliable UK and US Web Hosting @rshosting.com
Cheap US and UK Web Hosting @infrenion.com
I.T Certification Exams @pass-guaranteed.com
Certification Practice Tests @firsttrycertify.com
Certification Practice Exam Questions @ace-it-certifications.com
Broadband Speed Test @broadbandspeed.co.uk
bulk email software @americaint.com