| 148 |
lars |
1 |
String Template Resources {#resources.string}
|
|
|
2 |
=========================
|
|
|
3 |
|
|
|
4 |
Smarty can render templates from a string by using the `string:` or
|
|
|
5 |
`eval:` resource.
|
|
|
6 |
|
|
|
7 |
- The `string:` resource behaves much the same as a template file. The
|
|
|
8 |
template source is compiled from a string and stores the compiled
|
|
|
9 |
template code for later reuse. Each unique template string will
|
|
|
10 |
create a new compiled template file. If your template strings are
|
|
|
11 |
accessed frequently, this is a good choice. If you have frequently
|
|
|
12 |
changing template strings (or strings with low reuse value), the
|
|
|
13 |
`eval:` resource may be a better choice, as it doesn\'t save
|
|
|
14 |
compiled templates to disk.
|
|
|
15 |
|
|
|
16 |
- The `eval:` resource evaluates the template source every time a page
|
|
|
17 |
is rendered. This is a good choice for strings with low reuse value.
|
|
|
18 |
If the same string is accessed frequently, the `string:` resource
|
|
|
19 |
may be a better choice.
|
|
|
20 |
|
|
|
21 |
> **Note**
|
|
|
22 |
>
|
|
|
23 |
> With a `string:` resource type, each unique string generates a
|
|
|
24 |
> compiled file. Smarty cannot detect a string that has changed, and
|
|
|
25 |
> therefore will generate a new compiled file for each unique string. It
|
|
|
26 |
> is important to choose the correct resource so that you do not fill
|
|
|
27 |
> your disk space with wasted compiled strings.
|
|
|
28 |
|
|
|
29 |
|
|
|
30 |
<?php
|
|
|
31 |
$smarty->assign('foo','value');
|
|
|
32 |
$template_string = 'display {$foo} here';
|
|
|
33 |
$smarty->display('string:'.$template_string); // compiles for later reuse
|
|
|
34 |
$smarty->display('eval:'.$template_string); // compiles every time
|
|
|
35 |
?>
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
From within a Smarty template
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
{include file="string:$template_string"} {* compiles for later reuse *}
|
|
|
43 |
{include file="eval:$template_string"} {* compiles every time *}
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
Both `string:` and `eval:` resources may be encoded with
|
|
|
49 |
[`urlencode()`](&url.php-manual;urlencode) or
|
|
|
50 |
[`base64_encode()`](&url.php-manual;urlencode). This is not necessary
|
|
|
51 |
for the usual use of `string:` and `eval:`, but is required when using
|
|
|
52 |
either of them in conjunction with
|
|
|
53 |
[`Extends Template Resource`](#resources.extends)
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
<?php
|
|
|
57 |
$smarty->assign('foo','value');
|
|
|
58 |
$template_string_urlencode = urlencode('display {$foo} here');
|
|
|
59 |
$template_string_base64 = base64_encode('display {$foo} here');
|
|
|
60 |
$smarty->display('eval:urlencode:'.$template_string_urlencode); // will decode string using urldecode()
|
|
|
61 |
$smarty->display('eval:base64:'.$template_string_base64); // will decode string using base64_decode()
|
|
|
62 |
?>
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
From within a Smarty template
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
{include file="string:urlencode:$template_string_urlencode"} {* will decode string using urldecode() *}
|
|
|
70 |
{include file="eval:base64:$template_string_base64"} {* will decode string using base64_decode() *}
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|