Reputation: 11
I need to build table with features in JS and display it on .tpl. I want to transfer array from smarty to JS. At first, i try with variable:
{literal}<script language="javascript" type="text/javascript">
<!-- // variable="{/literal}{$product->name|escape:'html':'UTF-8'}{literal}";
// --> </script>{/literal}
and it work's. Than i try with array:
{literal}<script language="javascript" type="text/javascript">
<!-- // array="{/literal}{$features|json_encode}{literal}";
// --> </script>{/literal}
and this solution is not working. Do you have any ideas how can I build array in JS from array in smarty?
Upvotes: 1
Views: 444
Reputation: 2987
Taken from usage in other tpl in Prestashop, you can do it this way:
<script type="text/javascript">
taxesArray = new Array();
{foreach $taxesRatesByGroup as $tax_by_group}
taxesArray[{$tax_by_group.id_tax_rules_group}] = {$tax_by_group|json_encode};
{/foreach}
</script>
or for the features example you gave, should be something like:
<script type="text/javascript">
featuresArray = new Array();
{foreach $features key=k item=f}
featuresArray[{$k}] = {$f|json_encode};
{/foreach}
</script>
Upvotes: 1