| 148 |
lars |
1 |
File Template Resources {#resources.file}
|
|
|
2 |
=======================
|
|
|
3 |
|
|
|
4 |
Smarty ships with a built-in template resource for the filesystem. The
|
|
|
5 |
`file:` is the default resource. The resource key `file:` must only be
|
|
|
6 |
specified, if the
|
|
|
7 |
[`$default_resource_type`](#variable.default.resource.type) has been
|
|
|
8 |
changed.
|
|
|
9 |
|
|
|
10 |
If the file resource cannot find the requested template, the
|
|
|
11 |
[`$default_template_handler_func`](#variable.default.template.handler.func)
|
|
|
12 |
is invoked.
|
|
|
13 |
|
|
|
14 |
> **Note**
|
|
|
15 |
>
|
|
|
16 |
> As of Smarty 3.1 the file resource no longer walks through the
|
| 991 |
lars |
17 |
> [include\_path](https://www.php.net/ini.core.php#ini.include-path) unless
|
| 148 |
lars |
18 |
> [`$use_include_path` is activated](#variable.use.include.path)
|
|
|
19 |
|
|
|
20 |
Templates from \$template\_dir {#templates.from.template.dir}
|
|
|
21 |
------------------------------
|
|
|
22 |
|
|
|
23 |
The file resource pulls templates source files from the directories
|
|
|
24 |
specified in [`$template_dir`](#variable.template.dir). The list of
|
|
|
25 |
directories is traversed in the order they appear in the array. The
|
|
|
26 |
first template found is the one to process.
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
<?php
|
|
|
30 |
$smarty->display('index.tpl');
|
|
|
31 |
$smarty->display('file:index.tpl'); // same as above
|
|
|
32 |
?>
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
|
36 |
From within a Smarty template
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
{include file='index.tpl'}
|
|
|
40 |
{include file='file:index.tpl'} {* same as above *}
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
Templates from a specific \$template\_dir {#templates.from.specified.template.dir}
|
|
|
45 |
-----------------------------------------
|
|
|
46 |
|
|
|
47 |
Smarty 3.1 introduced the bracket-syntax for specifying an element from
|
|
|
48 |
[`$template_dir`](#variable.template.dir). This allows websites
|
|
|
49 |
employing multiple sets of templates better control over which template
|
|
|
50 |
to access.
|
|
|
51 |
|
|
|
52 |
The bracket-syntax can be used from anywhere you can specify the `file:`
|
|
|
53 |
resource type.
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
<?php
|
|
|
57 |
|
|
|
58 |
// setup template directories
|
|
|
59 |
$smarty->setTemplateDir(array(
|
|
|
60 |
'./templates', // element: 0, index: 0
|
|
|
61 |
'./templates_2', // element: 1, index: 1
|
|
|
62 |
'10' => 'templates_10', // element: 2, index: '10'
|
|
|
63 |
'foo' => 'templates_foo', // element: 3, index: 'foo'
|
|
|
64 |
));
|
|
|
65 |
|
|
|
66 |
/*
|
|
|
67 |
assume the template structure
|
|
|
68 |
./templates/foo.tpl
|
|
|
69 |
./templates_2/foo.tpl
|
|
|
70 |
./templates_2/bar.tpl
|
|
|
71 |
./templates_10/foo.tpl
|
|
|
72 |
./templates_10/bar.tpl
|
|
|
73 |
./templates_foo/foo.tpl
|
|
|
74 |
*/
|
|
|
75 |
|
|
|
76 |
// regular access
|
|
|
77 |
$smarty->display('file:foo.tpl');
|
|
|
78 |
// will load ./templates/foo.tpl
|
|
|
79 |
|
|
|
80 |
// using numeric index
|
|
|
81 |
$smarty->display('file:[1]foo.tpl');
|
|
|
82 |
// will load ./templates_2/foo.tpl
|
|
|
83 |
|
|
|
84 |
// using numeric string index
|
|
|
85 |
$smarty->display('file:[10]foo.tpl');
|
|
|
86 |
// will load ./templates_10/foo.tpl
|
|
|
87 |
|
|
|
88 |
// using string index
|
|
|
89 |
$smarty->display('file:[foo]foo.tpl');
|
|
|
90 |
// will load ./templates_foo/foo.tpl
|
|
|
91 |
|
|
|
92 |
// using "unknown" numeric index (using element number)
|
|
|
93 |
$smarty->display('file:[2]foo.tpl');
|
|
|
94 |
// will load ./templates_10/foo.tpl
|
|
|
95 |
|
|
|
96 |
?>
|
|
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
From within a Smarty template
|
|
|
101 |
|
|
|
102 |
|
|
|
103 |
{include file="file:foo.tpl"}
|
|
|
104 |
{* will load ./templates/foo.tpl *}
|
|
|
105 |
|
|
|
106 |
{include file="file:[1]foo.tpl"}
|
|
|
107 |
{* will load ./templates_2/foo.tpl *}
|
|
|
108 |
|
|
|
109 |
{include file="file:[foo]foo.tpl"}
|
|
|
110 |
{* will load ./templates_foo/foo.tpl *}
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
Templates from any directory {#templates.from.any.dir}
|
|
|
115 |
----------------------------
|
|
|
116 |
|
|
|
117 |
Templates outside of the [`$template_dir`](#variable.template.dir)
|
|
|
118 |
require the `file:` template resource type, followed by the absolute
|
|
|
119 |
path to the template (with leading slash.)
|
|
|
120 |
|
|
|
121 |
> **Note**
|
|
|
122 |
>
|
|
|
123 |
> With [`Security`](#advanced.features.security) enabled, access to
|
|
|
124 |
> templates outside of the [`$template_dir`](#variable.template.dir) is
|
|
|
125 |
> not allowed unless you list those directories in `$secure_dir`.
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
<?php
|
|
|
129 |
$smarty->display('file:/export/templates/index.tpl');
|
|
|
130 |
$smarty->display('file:/path/to/my/templates/menu.tpl');
|
|
|
131 |
?>
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
And from within a Smarty template:
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
{include file='file:/usr/local/share/templates/navigation.tpl'}
|
|
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
Windows Filepaths {#templates.windows.filepath}
|
|
|
143 |
-----------------
|
|
|
144 |
|
|
|
145 |
If you are using a Windows machine, filepaths usually include a drive
|
|
|
146 |
letter (C:) at the beginning of the pathname. Be sure to use `file:` in
|
|
|
147 |
the path to avoid namespace conflicts and get the desired results.
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
<?php
|
|
|
151 |
$smarty->display('file:C:/export/templates/index.tpl');
|
|
|
152 |
$smarty->display('file:F:/path/to/my/templates/menu.tpl');
|
|
|
153 |
?>
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
And from within Smarty template:
|
|
|
158 |
|
|
|
159 |
|
|
|
160 |
{include file='file:D:/usr/local/share/templates/navigation.tpl'}
|