aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/smarty/smarty/docs/programmers/resources/resources-file.md
blob: 986cfffcad797c4ec14afa15a9e9e4fdaaed00c7 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
File Template Resources {#resources.file}
=======================

Smarty ships with a built-in template resource for the filesystem. The
`file:` is the default resource. The resource key `file:` must only be
specified, if the
[`$default_resource_type`](#variable.default.resource.type) has been
changed.

If the file resource cannot find the requested template, the
[`$default_template_handler_func`](#variable.default.template.handler.func)
is invoked.

> **Note**
>
> As of Smarty 3.1 the file resource no longer walks through the
> [include\_path](&url.php-manual;ini.core.php#ini.include-path) unless
> [`$use_include_path` is activated](#variable.use.include.path)

Templates from \$template\_dir {#templates.from.template.dir}
------------------------------

The file resource pulls templates source files from the directories
specified in [`$template_dir`](#variable.template.dir). The list of
directories is traversed in the order they appear in the array. The
first template found is the one to process.


    <?php
    $smarty->display('index.tpl');
    $smarty->display('file:index.tpl'); // same as above
    ?>

       

From within a Smarty template


    {include file='index.tpl'}
    {include file='file:index.tpl'} {* same as above *}

       

Templates from a specific \$template\_dir {#templates.from.specified.template.dir}
-----------------------------------------

Smarty 3.1 introduced the bracket-syntax for specifying an element from
[`$template_dir`](#variable.template.dir). This allows websites
employing multiple sets of templates better control over which template
to acces.

The bracket-syntax can be used from anywhere you can specify the `file:`
resource type.


    <?php

    // setup template directories
    $smarty->setTemplateDir(array(
        './templates',            // element: 0, index: 0
        './templates_2',          // element: 1, index: 1
        '10' => 'templates_10',   // element: 2, index: '10'
        'foo' => 'templates_foo', // element: 3, index: 'foo'
    ));

    /*
      assume the template structure
      ./templates/foo.tpl
      ./templates_2/foo.tpl
      ./templates_2/bar.tpl
      ./templates_10/foo.tpl
      ./templates_10/bar.tpl
      ./templates_foo/foo.tpl
    */

    // regular access
    $smarty->display('file:foo.tpl'); 
    // will load ./templates/foo.tpl

    // using numeric index
    $smarty->display('file:[1]foo.tpl'); 
    // will load ./templates_2/foo.tpl

    // using numeric string index
    $smarty->display('file:[10]foo.tpl'); 
    // will load ./templates_10/foo.tpl

    // using string index
    $smarty->display('file:[foo]foo.tpl'); 
    // will load ./templates_foo/foo.tpl

    // using "unknown" numeric index (using element number)
    $smarty->display('file:[2]foo.tpl'); 
    // will load ./templates_10/foo.tpl

    ?>

       

From within a Smarty template


    {include file="file:foo.tpl"}
    {* will load ./templates/foo.tpl *}

    {include file="file:[1]foo.tpl"}
    {* will load ./templates_2/foo.tpl *}

    {include file="file:[foo]foo.tpl"}
    {* will load ./templates_foo/foo.tpl *}

       

Templates from any directory {#templates.from.any.dir}
----------------------------

Templates outside of the [`$template_dir`](#variable.template.dir)
require the `file:` template resource type, followed by the absolute
path to the template (with leading slash.)

> **Note**
>
> With [`Security`](#advanced.features.security) enabled, access to
> templates outside of the [`$template_dir`](#variable.template.dir) is
> not allowed unless you list those directories in `$secure_dir`.


    <?php
    $smarty->display('file:/export/templates/index.tpl');
    $smarty->display('file:/path/to/my/templates/menu.tpl');
    ?>

       

And from within a Smarty template:


    {include file='file:/usr/local/share/templates/navigation.tpl'}

       

Windows Filepaths {#templates.windows.filepath}
-----------------

If you are using a Windows machine, filepaths usually include a drive
letter (C:) at the beginning of the pathname. Be sure to use `file:` in
the path to avoid namespace conflicts and get the desired results.


    <?php
    $smarty->display('file:C:/export/templates/index.tpl');
    $smarty->display('file:F:/path/to/my/templates/menu.tpl');
    ?>

      

And from within Smarty template:


    {include file='file:D:/usr/local/share/templates/navigation.tpl'}