What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
Table of Contents
Smarty は、テンプレートで使用可能なカスタム関数をいくつか実装しています。
{assign}
は、テンプレート変数を
テンプレートの実行時に
割り当てます。
属性名 | 型 | 必須 | デフォルト | 概要 |
---|---|---|---|---|
var | string | Yes | n/a | 割り当てられるテンプレート変数の名前 |
value | string | Yes | n/a | テンプレート変数に割り当てる値 |
Example 8.1. {assign}
{assign var='name' value='Bob'} The value of $name is {$name}.
上の例の出力
The value of $name is Bob.
Example 8.2. {assign} での演算子の使用
この複雑な例では、変数を `バッククォート` で囲む必要があります。
{assign var=running_total value=`$running_total+$some_array[row].some_value`}
Example 8.3. PHP スクリプトからの {assign} 変数へのアクセス
PHP スクリプトから {assign}
変数にアクセスするには
get_template_vars()
を使用します。これは、変数 $foo
を作成するテンプレートです。
{assign var='foo' value='Smarty'}
テンプレート変数は、以下のスクリプトのように テンプレートの実行後か実行中にしか利用できません。
<?php // これは何も出力しません。テンプレートがまだ実行されていないからです。 echo $smarty->get_template_vars('foo'); // テンプレートを変数に格納します。 $whole_page = $smarty->fetch('index.tpl'); // これは 'smarty' と出力します。テンプレートが実行されたからです。 echo $smarty->get_template_vars('foo'); $smarty->assign('foo','Even smarter'); // これは 'Even smarter' を出力します。 echo $smarty->get_template_vars('foo'); ?>
次の関数も、オプションで テンプレート変数へ割り当てることができます。
{capture}
、
{include}
、
{include_php}
、
{insert}
、
{counter}
、
{cycle}
、
{eval}
、
{fetch}
、
{math}
、
{textformat}
assign()
および
get_template_vars()
も参照してください。