逃离Smarty 2的JS和PHP块

时间:2016-12-01 15:44:43

标签: javascript php cs-cart smarty2

稍后编辑:此问题已得到修复。似乎问题在于错误的钩子。

在Smarty 2中逃避JavaScript和PHP块的正确和最佳方法是什么?

我试过:

  • 使用{ldelim} respectiely {rdelim};
  • 更改每个JS {}
  • 之前添加{literal},
  • 之后添加{/ literal}
  • 添加{php} phpcodehere {/ php}

它们似乎都不起作用。

以下是我需要转义的代码:

<script>
    var _ra = _ra || {};
    _ra.sendProductInfo = {
        "id": "{$product.product_id}",
        "name": "{$product.product}",
        "url": "{$config.current_url|fn_url}",
        "img": "{$product.main_pair.detailed.image_path}",
        {if $ra_oldPrice == $product.price or not is_numeric($ra_oldPrice)}
        "price": "{if $product.list_price > $product.price}{$product.list_price}{else}{$product.price}{/if}",
        "promo": "{if $product.list_price > $product.price}{$product.price}{else}0{/if}",
        {else}
        "price": "{$ra_oldPrice}",
        "promo": "{$product.price}",
        {/if}
        "brand": false,
        "category": [{
            "id": "{$catid}",
            "name": "{$product.main_category|fn_get_category_name}",
            "parent": false,
            "breadcrumb": []
        }],
        "inventory": {
            "variations": false,
            "stock": "{$product_amount = $product.inventory_amount|default:$product.amount}{if ($product_amount <= 0 || $product_amount < $product.min_qty) && $settings.General.inventory_tracking == "Y"}0{else}1{/if}"   
        }
    };
</script>

1 个答案:

答案 0 :(得分:0)

请尝试:

<script data-no-defer> 
    var _ra = _ra || {$ldelim}{$rdelim};
    _ra.sendProductInfo = {$ldelim}
        id: {$product.product_id},
        name: '{$product.product|escape:'javascript'}',
        url: '{$config.current_url|fn_url}',
        img: '{$product.main_pair.detailed.image_path}',
        {if $ra_oldPrice == $product.price or not is_numeric($ra_oldPrice)}
        price: {if $product.list_price > $product.price}{$product.list_price}{else}{$product.price}{/if},
        promo: {if $product.list_price > $product.price}{$product.price}{else}0{/if},
        {else}
        price: {$ra_oldPrice},
        promo: {$product.price},
        {/if}
        brand: false,
        category: [{$ldelim}
            id: {$product.main_category},
            name : '{$category_name=$product.main_category|fn_get_category_name}{$category_name|escape:'javascript'}',
            parent: false,
            breadcrumb: []
        {$rdelim}],
        inventory: {$ldelim}
            variations: false,
            stock: {$product_amount = $product.inventory_amount|default:$product.amount}{if ($product_amount <= 0 || $product_amount < $product.min_qty) && $settings.General.inventory_tracking == "Y"}0{else}1{/if}   
        {$rdelim}
    {$rdelim};
</script>
  • 我唯一不知道的变量是$ra_oldPrice,但我猜是上面由你指定