johan855 johan855 - 4 months ago 13
Smarty Question

Smarty syntax for turning a dictionary into a string and push it into the data layer

I am trying to find a way to pass a string variable (coming from a dictionary) into my website's data layer.

My array is built using the following snippet:

[{foreach from=$orderArticles item="currOrderArticle"}]
[{assign var="currBasePrice2" value=$currOrderArticle-getBasePrice()}]
product_list.push(
{
id: "[{$currOrderArticle-oxorderarticles__oxartnum->value}]",
price: [{$currBasePrice2->getBruttoPrice()}],
quantity: [{$currOrderArticle-oxorderarticles__oxamount->value}]
}
);
products_info.push(
{
transaction_id: '[{$order->oxorder__oxordernr-value}]',
transaction_cid: '[{$order->oxorder__oxuserid-value}]',
item_id: '[{$currOrderArticle-oxorderarticles__oxartnum->value}]',
item_value: '[{$basket-getDiscountedNettoPrice()}]',
item_quantity: '[{$currOrderArticle-oxorderarticles__oxamount->value}]'
}
);
[{/foreach}]


If I want to pass it to the data layer, I do the following:

dataLayer.push({
ProductsInfo: products_info


});

This works fine, the issue is that I actually want to modify this array.
I would like to apply the following to it before passing it into the data layer:

products_info|json_encode|escape:'url'


But when I try to do it during the push, it does not work:

dataLayer.push({
ProductsInfo: products_info|json_encode|escape:'url'
});

Answer

As I told you in another post, Smarty expressions must be enclosed in delimiters, in your case [{ and }]

Also, as you're using the string in javascript, it must be enclosed within quotes:

 dataLayer.push({
        ProductsInfo: '[{products_info|json_encode|escape:'url'}]'
});
Comments