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:
テンプレートの大半が同じヘッダ及びフッタを使用する場合は、それらを単体のテンプレートに分割して
{include}
するのが普通です。
しかしどのページから呼び出されたかによって、
そのヘッダに異なるタイトルを持たせる必要があるとすればどうなるでしょうか?
インクルードされる際に、タイトルを
属性
としてヘッダに渡す事ができます。
Example 18.3. ヘッダテンプレートにタイトルの変数を渡す
mainpage.tpl
- メインページを描画する際に、
“Main Page” というタイトルを
header.tpl
に私、それをタイトルとして使用します。
{include file='header.tpl' title='Main Page'} {* ここにテンプレートの本体を記述します *} {include file='footer.tpl'}
archives.tpl
- アーカイブページを描画する際には、
タイトルは “Archives” となります。
この例では、ハードコーディングされた変数ではなく
archives_page.conf
から変数を取得していることに注意しましょう。
{config_load file='archive_page.conf'} {include file='header.tpl' title=#archivePageTitle#} {* ここにテンプレートの本体を記述します *} {include file='footer.tpl'}
header.tpl
- $title
変数が設定されていない場合に、
“Smarty News” と表示します。これは
default
修飾子を使用して実現しています。
<html> <head> <title>{$title|default:'Smarty News'}</title> </head> <body>
footer.tpl
</body> </html>