aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/smarty/smarty/docs/programmers/advanced-features/advanced-features-template-inheritance.md
blob: 25295c38dff889b6a2500d6bfe93b92d12fd9756 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
Template Inheritance {#advanced.features.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}`](#language.function.block) tags they override.
    Anything outside of [`{block}`](#language.function.block) tags will
    be removed.

-   The content of [`{block}`](#language.function.block) tags from child
    and parent templates can be merged by the `append` or `prepend`
    [`{block}`](#language.function.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}`](#language.function.include) tag it does have much
    better performance when rendering.

-   The child template extends its parent defined with the
    [`{extends}`](#language.function.extends) tag, which must be the
    first line in the child template. Instead of using the
    [`{extends}`](#language.function.extends) tags in the template files
    you can define the whole template inheritance tree in the PHP script
    when you are calling [`fetch()`](#api.fetch) or
    [`display()`](#api.display) with the `extends:` template resource
    type. The later provides even more flexibillity.

> **Note**
>
> 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.

> **Note**
>
> If you have a subtemplate which is included with
> [`{include}`](#language.function.include) and it contains
> [`{block}`](#language.function.block) areas it works only if the
> [`{include}`](#language.function.include) itself is called from within
> a surrounding [`{block}`](#language.function.block). In the final
> parent template you may need a dummy
> [`{block}`](#language.function.block) for it.

layout.tpl (parent)


    <html>
    <head>
      <title>{block name=title}Default Page Title{/block}</title>
      {block name=head}{/block}
    </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}


      

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}
    {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>

Instead of using [`{extends}`](#language.function.extends) tags in the
template files you can define the inheritance tree in your PHP script by
using the [`extends:` resource](#resources.extends) 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}`](#language.function.block),
[`{extends}`](#language.function.extends) and [`extends:`
resource](#resources.extends)