Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
Escaping Smarty Parsing {#language.escaping}
2
=======================
3
 
4
It is sometimes desirable or even necessary to have Smarty ignore
5
sections it would otherwise parse. A classic example is embedding
6
Javascript or CSS code in a template. The problem arises as those
7
languages use the { and } characters which are also the default
8
[delimiters](#language.function.ldelim) for Smarty.
9
 
10
> **Note**
11
>
12
> A good practice for avoiding escapement altogether is by separating
13
> your Javascript/CSS into their own files and use standard HTML methods
14
> to access them. This will also take advantage of browser script
15
> caching. When you need to embed Smarty variables/functions into your
16
> Javascript/CSS, then the following applies.
17
 
18
In Smarty templates, the { and } braces will be ignored so long as they
19
are surrounded by white space. This behavior can be disabled by setting
20
the Smarty class variable [`$auto_literal`](#variable.auto.literal) to
21
false.
22
 
23
 
24
    <script>
25
       // the following braces are ignored by Smarty
26
       // since they are surrounded by whitespace
27
       function foobar {
28
        alert('foobar!');
29
       }
30
       // this one will need literal escapement
31
       {literal}
32
        function bazzy {alert('foobar!');}
33
       {/literal}
34
    </script>
35
 
36
 
37
 
38
[`{literal}..{/literal}`](#language.function.literal) blocks are used
39
for escaping blocks of template logic. You can also escape the braces
40
individually with
41
[`{ldelim}`](#language.function.ldelim),[`{rdelim}`](#language.function.ldelim)
42
tags or
43
[`{$smarty.ldelim}`,`{$smarty.rdelim}`](#language.variables.smarty.ldelim)
44
variables.
45
 
46
Smarty\'s default delimiters { and } cleanly represent presentational
47
content. However if another set of delimiters suit your needs better,
48
you can change them with Smarty\'s
49
[`$left_delimiter`](#variable.left.delimiter) and
50
[`$right_delimiter`](#variable.right.delimiter) values.
51
 
52
> **Note**
53
>
54
> Changing delimiters affects ALL template syntax and escapement. Be
55
> sure to clear out cache and compiled files if you decide to change
56
> them.
57
 
58
 
59
    <?php
60
 
61
    $smarty->left_delimiter = '<!--{';
62
    $smarty->right_delimiter = '}-->';
63
 
64
    $smarty->assign('foo', 'bar');
65
    $smarty->assign('name', 'Albert');
66
    $smarty->display('example.tpl');
67
 
68
    ?>
69
 
70
 
71
 
72
Where the template is:
73
 
74
 
75
    Welcome <!--{$name}--> to Smarty
76
    <script language="javascript">
77
      var foo = <!--{$foo}-->;
78
      function dosomething() {
79
        alert("foo is " + foo);
80
      }
81
      dosomething();
82
    </script>
83
 
84