Inheritance brings the concept of Object Oriented Programming to templates, allowing you to define one (or more) base templates that can be extended by child templates. Extending means that the child template can override all or some of the parent named block areas.
The inheritance tree can be as big as you want (meaning you can extend a file that extends another one that extends another one and so on..), but be aware that all files have to be checked for modifications at runtime so the more inheritance the more overhead you add.
The child templates can not define any content besides what's inside {block}
tags they override, anything outside of {block}
tags will be removed.
The content of {block}
tags from child and parent templates can be merged by
the append
or prepend
{block}
tag option flags and {$smarty.block.parent}
or {$smarty.block.child}
placeholders.
Template inheritance is a compile time process which does create a single compiled template file. Compared to corresponding
solutions based on subtemplates included with the {include}
tag it does have much better performance when redering.
The child template does extend its parent defined with the {extends}
tag,
which must be the first line in the child template. Instead of using the {extends}
tags in the template files you can define the whole template inheritance tree in the PHP script when you are calling fetch()
or display()
with the extends:
template resource type. The later provides even more flexibillity.
If you have a subtemplate which is included with {include}
and it does contain
{block}
areas it does work only if the {include}
itself is called from within a surrounding {block}
. In the final parent template you may need a dummy {block}
for it.
Example 15.6. Template inheritance example
layout.tpl (parent)
<html> <head> <title>{block name=title}Default Page Title{/block}</title> <span style="color: blue">{block name=head}{/block}</span> </head> <body> {block name=body}{/block} </body> </html>
myproject.tpl (child)
{extends file=layout.tpl} {block name=head} <link href="/css/mypage.css" rel="stylesheet" type="text/css"/> <script src="/js/mypage.js"></script> {/block}
myproject.tpl (grandchild)
{extends file=project.tpl} {block name=title}My Page Title{/block} {block name=head} <link href="/css/mypage.css" rel="stylesheet" type="text/css"/> <script src="/js/mypage.js"></script> {/block} {block name=body}My HTML Page Body goes here{/block}
To render the above use
$smarty->display('mypage.tpl');
The resulting output is
<html> <head> <title>My Page Title</title> <link href="/css/mypage.css" rel="stylesheet" type="text/css"/> <script src="/js/mypage.js"></script> </head> <body> My HTML Page Body goes here </body> </html>
Example 15.7. Template inheritance by template resource extends:
Instead of using {extends}
tags in the template
files you can define the inheritance tree in your PHP script by using the extends:
resource type.
The code below will return same result as the example above.
<?php $smarty->display('extends:layout.tpl|myproject.tpl|mypage.tpl'); ?>
See also
{block}
,
{extends}
and extends:
resource