Sometimes you will want to output a curly brace directly in your template without it having anything to do with template code. This may arise if you want to output CSS or JavaScript in your template directly.
Using code such as the following will break your template, because the template interpreter reads the curly braces and thinks they are template instructions.
Example A.12. Sample scenario of using non-template curly braces.
<style type="text/css"> body { background : #fff; } </style> <script type="text/javascript"> function foo() { // do something } </script>
As an aside, it is good practice to never embed CSS or JavaScript directly in a HTML page. It is almost never correct to do so. Rather, you should use external CSS or JavaScript files.
To get around this, you can either wrap your CSS/JavaScript in
{literal}
, or you can output a left brace with {ldelim}
and a
right brace with {rdelim}
.
The {literal}
function instructs the template interpreter to ignore
everything between the opening and closing tag. The following listing demonstrates this.
Example A.13. Using {literal} to embed curly braces.
{literal} <style type="text/css"> body { background : #fff; } </style> <script type="text/javascript"> function foo() { // do something } </script> {/literal}
Sometimes you will want to output a variable in your template but still use a curly brace. The following listing shows how you can combine JavaScript code with your template.
Example A.14. Using {ldelim} and {rdelim}.
<script type="text/javascript"> function foo() {ldelim} var numberOfArticles = {$articles|@count}; {rdelim} </script>