PHP » Smarty
How do I emit curly braces while working with Smarty?
By default Smarty considers curly braces as delimiters for its own compilation.
So, if you want to have a JavaScript code, as given below, in the template file:
<script language='javascript' type='text/javascript'>
function doWork() {
var obj = { name: 'Edujini FAQs',
url: 'http://faqs.edujini-labs.com' };
alert(obj.name + ' => ' + obj.url);
}
</script>
Smarty will complain about invalid types or keyword "{ var" etc because it considers anything within curly braces as a code for execution.
There are multiple solutions to this problem, though trivial but encountered several times:
- Use literal text: http://www.smarty.net/manual/en/language.function.literal.php
- Use delimiters: http://www.smarty.net/manual/en/language.function.ldelim.php
Simplest solution is to include your entire content within a literal block.
{literal}
<script language='javascript' type='text/javascript'>
function doWork() {
var obj = { name: 'Edujini FAQs',
url: 'http://faqs.edujini-labs.com' };
alert(obj.name + ' => ' + obj.url);
}
</script>
{/literal}In the second option, you may emit out ldelim / delim functions or directly left_delimiter / right_delimiter variables. The usage is demonstrated in the code below:
<script language='javascript' type='text/javascript'>
function doWork() {ldelim}
var obj = {ldelim} name: 'Edujini FAQs',
url: 'http://faqs.edujini-labs.com' {rdelim};
alert(obj.name + ' => ' + obj.url);
{rdelim}
</script>
<script language='javascript' type='text/javascript'>
function doWork() {$left_delimiter}
var obj = {$left_delimiter} name: 'Edujini FAQs',
url: 'http://faqs.edujini-labs.com' {$right_delimiter};
alert(obj.name + ' => ' + obj.url);
{$right_delimiter}
</script>
And you are ready to go....
Last update: 2008-06-26 00:14
Author: Edujini Labs Pvt Ltd
Revision: 1.0
Print this record
Send to a friend
Show this as PDF file
Export as XML-File
You can comment on this entry
Comment of YyAXdgFybilSUvX (2011-10-28 22:34:09):
Son of a gun, this is so helpufl!