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:
{block}
は、テンプレートの継承用に、
テンプレートソースのある範囲に名前を定義します。詳細は
テンプレートの継承
の節を参照ください。
{block}
で囲んだ子テンプレートの範囲は、
親テンプレートの対応する部分を置き換えます。
オプションで、子テンプレートと親テンプレートの {block}
範囲をマージすることもできます。親の {block}
のコンテンツを子の {block}
の前あるいは後に加えるには、
子の {block}
を定義する際にオプションのフラグ
append
あるいは prepend
を指定します。
{$smarty.block.parent} を使うと、親テンプレートの {block}
の中身を子テンプレートの {block}
内の好きな場所に挿入することができます。
また {$smarty.block.child} は、子テンプレートの {block}
の中身を親テンプレートの {block}
内の好きな場所に挿入することができます。
{blocks}
はネストすることができます。
属性
属性名 | 型 | 必須 | デフォルト | 概要 |
---|---|---|---|---|
name | string | Yes | n/a | テンプレートソースブロックの名前 |
オプションのフラグ (子テンプレートのみ)
名前 | 概要 |
---|---|
append |
{block} のコンテンツを親テンプレートの {block} に追記する |
prepend |
{block} のコンテンツを親テンプレートの {block} の前に置く |
nocache |
{block} のコンテンツのキャッシュを無効にする |
Example 7.15. シンプルな {block}
の例
parent.tpl
<html> <head> <title>{block name="title"}Default Title{/block}</title> <title>{block "title"}Default Title{/block}</title> {* 短縮形 *} </head> </html>
child.tpl
{extends file="parent.tpl"} {block name="title"} Page Title {/block}
結果は、このようになります。
<html> <head> <title>Page Title</title> </head> </html>
Example 7.16. {block}
を前に置く例
parent.tpl
<html> <head> <title>{block name="title"}Title - {/block}</title> </head> </html>
child.tpl
{extends file="parent.tpl"} {block name="title" prepend} Page Title {/block}
結果は、このようになります。
<html> <head> <title>Title - Page Title</title> </head> </html>
Example 7.17. {block}
を後に置く例
parent.tpl
<html> <head> <title>{block name="title"} is my titel{/block}</title> </head> </html>
child.tpl
{extends file="parent.tpl"} {block name="title" append} Page Title {/block}
結果は、このようになります。
<html> <head> <title>Page title is my titel</title> </head> </html>
Example 7.18. {$smarty.block.child}
の例
parent.tpl
<html> <head> <title>{block name="title"}The {$smarty.block.child} was inserted here{/block}</title> </head> </html>
child.tpl
{extends file="parent.tpl"} {block name="title" append} Child Title {/block}
結果は、このようになります。
<html> <head> <title>The - Child Title - was inserted here</title> </head> </html>
Example 7.19. {$smarty.block.parent}
の例
parent.tpl
<html> <head> <title>{block name="title"}Parent Title{/block}</title> </head> </html>
child.tpl
{extends file="parent.tpl"} {block name="title" append} You will see now - {$smarty.block.parent} - here {/block}
結果は、このようになります。
<html> <head> <title>You will see now - Parent Title - here</title> </head> </html>
テンプレートの継承、
$smarty.block.parent
、
$smarty.block.child
および {extends}
も参照ください。