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

コンポーネント化したテンプレート

習慣的に、アプリケーションにテンプレートをプログラミングする手順は次のように進みます。 はじめに php アプリケーションにおいて変数を蓄積します (おそらくデータベースのクエリーによって)。それから Smarty オブジェクトのインスタンスを作成して変数を割り当て (assign())、 テンプレートを表示 (display()) します。仮に株式相場表示を行うテンプレートがあったとしましょう。 これは php アプリケーションにより株式情報のデータを収集し、 テンプレートにこれらの変数を割り当てて表示します。 もし、前もってデータを取得する事を気にせずに、 テンプレートを単にインクルードする事で株式相場表示をアプリケーションに追加できれば良いと思いませんか?

これは、内容をフェッチし、テンプレート変数に割り当てるための カスタムプラグインを書くことで実現できます

Example 18.7. コンポーネント化したテンプレート

function.load_ticker.php - このファイルを プラグインのディレクトリ においてください。


<?php

// 株式情報のデータを取得するための関数を用意します
function fetch_ticker($symbol)
{
   // 様々なリソースから $ticker_info を
   // 取得するロジックをここに記述します
   return $ticker_info;
}

function smarty_function_load_ticker($params, &$smarty)
{
   // 関数をコールします
   $ticker_info = fetch_ticker($params['symbol']);

   // テンプレート変数を割り当てます
   $smarty->assign($params['assign'], $ticker_info);
}
?>

    

index.tpl


{load_ticker symbol='SMARTY' assign='ticker'}

銘柄: {$ticker.name} 株価: {$ticker.price}

    

{include_php}{include} および {php} も参照してください。

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 18 plus 17? (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