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:
{eval}
は、与えられた変数をテンプレートとして評価します。
テンプレート変数又はテンプレートタグを
変数や設定ファイル内に埋め込むような用途に使われます。
assign
属性が指定されると、
{eval}
関数の出y録はこのテンプレート変数に割り当てられ、
テンプレートに出力されることはありません。
属性名 | 型 | 必須 | デフォルト | 概要 |
---|---|---|---|---|
var | mixed | Yes | n/a | 評価される変数 (又は文字列) |
assign | string | No | n/a | 出力が割り当てられるテンプレート変数 |
評価される変数は、テンプレートと同じように扱われます。 エスケープやセキュリティ機能も、テンプレートと同様になります。
評価される変数はリクエスト毎にコンパイルされるので、 コンパイルされた形式では保存されません。ですが、 キャッシュ が有効に設定されている場合は、 残りのテンプレートの出力に関してはキャッシュされます。
Example 8.3. {eval}
設定ファイル setup.conf
emphstart = <strong> emphend = </strong> title = Welcome to {$company}'s home page! ErrorCity = You must supply a {#emphstart#}city{#emphend#}. ErrorState = You must supply a {#emphstart#}state{#emphend#}.
テンプレート
{config_load file='setup.conf'} {eval var=$foo} {eval var=#title#} {eval var=#ErrorCity#} {eval var=#ErrorState# assign='state_error'} {$state_error}
上のテンプレートの出力
This is the contents of foo. Welcome to Foobar Pub & Grill's home page! You must supply a <strong>city</strong>. You must supply a <strong>state</strong>.
Example 8.4. もうひとつの {eval} の例
これは、サーバ名 (大文字変換したもの) と IP を出力します。
割り当てられる変数 $str
は、
データベースのクエリから取得します。
<?php $str = 'The server name is {$smarty.server.SERVER_NAME|upper} ' .'at {$smarty.server.SERVER_ADDR}'; $smarty->assign('foo',$str); ?>
テンプレート
{eval var=$foo}