Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

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