1

Im trying to use the key sign '}' inside a string of smarty template but it generate an error. The problem only ocurrs with '}' and not with '{´. I need print:

var naciones = [{label:'Country', value:'1'}, {label:'Country', value:'2'}];

solution:

var naciones = [{/literal}{foreach from=$paises item=pa}{literal}{label:"{/literal}{$pa->getNacionalidad()}{literal}", value:"{/literal}{$pa->getId()}{literal}"},{/literal}{/foreach}{literal}];

example:

{literal}
<script type="text/javascript">
var naciones = [{/literal}
{foreach from=$paises item=pa}
{'{label:"'|cat:$pa->getNacionalidad()|cat:'", value:"'|cat:$pa->getId()|cat:'"'}{cat:'"}, '}{/foreach}{literal}];
$('#nacionalidad-ac').autocomplete({
  source:naciones,
  change: function(event, ui){
    $('#nacionalidad').val(ui.item.value);
  }
});
</script>
{/literal}

thanks

2 Answers 2

2

You need the {literal} {/literal} tag to properly escape the curly braces in a Smarty template. Also necessary for inline Javascript which has braces.

2
  • Best to avoid inline Javascript anyway. Mar 14, 2011 at 16:22
  • thanks, i wrote: var naciones = [{/literal}{foreach from=$paises item=pa}{literal}{label:"{/literal}{$pa->getNacionalidad()}{literal}", value:"{/literal}{$pa->getId()}{literal}"},{/literal}{/foreach}{literal}];
    – returnvoid
    Mar 14, 2011 at 18:10
2

You can use {literal} to stop parsing. So a smarty template with a '{' would look like

This is just a text with a {$smartyString}
There is also an {literal} } {/literal} sign in here that could give you some trouble.
1
  • sure. im using literal tag but in my example there is a strange mix of opening/closed tags and i dont know how apply that principle.
    – returnvoid
    Mar 14, 2011 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.