Get Smarty


Donate Bitcoin Bitcoin

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:

Sites Using Smarty

Largest classified with thousands of ads at Shoppok

Buy cheap eyeglasses from and save up to 80%.

Buy prescription glasses from and save.

Cheap Glasses Now On Sale at Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping -

Find Wedding Dresses Online at Canada from

Find your local domestic cleaner

Brautkleider auf

Find free files to download on

Looking For Affordable Wedding Dresses 2015 at Best Prices On

Shop high quality cheap prom dresses on

Buy New Arrival Cheap Prom Dresses 2015 at JDBRIDAL Prom Dress Store


Template Inheritance

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 deep as you want, meaning you can extend a file that extends another one that extends another one and so on.

  • 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 creates a single compiled template file. Compared to corresponding solutions based on subtemplates included with the {include} tag it does have much better performance when rendering.

  • The child template extends 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.


When $compile_check is enabled, all files in the inheritance tree are checked for modifications upon each invocation. You may want to disable $compile_check on production servers for this reason.


If you have a subtemplate which is included with {include} and it contains {block} areas it works 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 17.6. Template inheritance example

layout.tpl (parent)

  <title>{block name=title}Default Page Title{/block}</title>
  {block name=head}{/block}
{block name=body}{/block}


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>


mypage.tpl (grandchild)

{extends file='myproject.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 name=body}My HTML Page Body goes here{/block}


To render the above use


The resulting output is

  <title>My Page Title</title>
  <link href="/css/mypage.css" rel="stylesheet" type="text/css"/>
  <script src="/js/mypage.js"></script>
My HTML Page Body goes here

Example 17.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.



See also {block}, {extends} and extends: resource

No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Email: (not shown)
What is 8 plus 20? (Are you human?)


Sponsors [info]

UK Web Hosting
Best Web Hosting
Web Hosting UK
Unlimited Web Hosting
App Entwicklung
First Click Internet Marketing
Credit Card Processing Company
Cleaning Services Chicago