首页 > 解决方案 > Prestashop 1.7 - when assigning a variable from Smarty to JS, single quotes are converted

问题描述

When assigning a string variable from Smarty 3.1.33 to JS, single quotes ' are converted to entities &#039 ;

My code:

{$newOrder = $newOrder|cat :"[{ldelim}\r\n'transactionId':'$orderID'\r\n"|cat: "'transactionAffiliation': 'site.com',\r\n"|cat: "'transactionTotal': '$totalPrice',\r\n"|cat :"'transactionProducts': $orderProducts\r\n {rdelim}]"}

<script>dataLayer = {$newOrder|escape:'html':'UTF-8'};</script>

Get:

<script>dataLayer = [{
    &#039;transactionId&#039;:&#039;7214&#039;
    &#039;transactionAffiliation&#039;: &#039;site.com&#039;,
    &#039;transactionTotal&#039;: &#039;608&nbsp;грн.&#039;,
    &#039;transactionProducts&#039;: [{
    &#039;sku&#039;:&#039;17962&#039;,
    &#039;name&#039;:&#039;Детские ходунки M 0591-S микс 4 цвета&#039;,
    &#039;category&#039;:&#039;Ходунки&#039;,
    &#039;price&#039;:445,
    &#039;quantity&#039;:1
}],
[{
    &#039;sku&#039;:&#039;28963&#039;,
    &#039;name&#039;:&#039;Планшет обучающий SK 0019&#039;,
    &#039;category&#039;:&#039;Говорящие азбуки&#039;,
    &#039;price&#039;:163,
    &#039;quantity&#039;:1
}]
 }];
    </script>

Need (without "&#039 ;"):

<script>dataLayer = [{
    'transactionId': '1234',                                                         
    'transactionAffiliation': 'site.com',
    'transactionTotal': 11.99,                                                     
    'transactionProducts': [{
    'sku': 'DD44',                                                                          
    'name': 'T-Shirt',                                                                    
    'category': 'Apparel',                                                             
    'price': 11.99,                                                                         
    'quantity': 1                                                                            
   }]
}];
</script>

标签: javascriptsmartyprestashop-1.7

解决方案


由于 prestashop 在 1.7+ 版本中更改了转义方式,因此引起了很多头痛。

您可以尝试像这样删除自动过滤器{$newOrder nofilter}


推荐阅读