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

ヘッダテンプレートにタイトルの変数を渡す

テンプレートの大半が同じヘッダ及びフッタを使用する場合は、それらを単体のテンプレートに分割して {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>

    

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 20 plus 17? (Are you human?)

Sponsors [info]

Cheap Website Hosting @hostdone.com
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
ASP.NET hosting @aspnethosting.co.uk