Usually if we need to use any dynamic value inside javascript, we just echo the value inside javascript as given below:

But in smarty it’ll pose some problem if we do like above. As per smarty documentation we should place the javascript code inside literal tag as shown below, for it to work.


But the above code only make javascript to work in the smarty template file. If we assign smarty variable to it like PHP variable, it won’t work. I’ve referred through lot of documentation and forums for the solution. They’ve mentioned to use {ldelim} and {rdelim} in between the smarty variables inside the javascript. But that too doesn’t work.

Finally I found out the solution in here. According to this thread, we’ve to escape the smarty variable inside javascript or jquery code with inverse {literal} tag. What do you mean by inverse {literal} tag? I know. Just place the closing {/literal} tag before the variable and opening {literal} tag after the variable like given below:

Thats it. Don’t think I placed closing {/literal} tag first. That’s how it works. Crazy smarty! Right!