通过模板覆盖js插件选项

问题描述 投票:0回答:1

我在覆盖 Twig 的 JS 插件选项时遇到问题。

我的 JS 插件选项如下所示:

static options = {
    configurator: {
        startTabIndex: 0,
        startModel: null,
        responsive: {
            xs: {
                startTabIndex: 1,
                startModel: 1
            },
            sm: {
                startTabIndex: 1,
                startModel: 1
            },
            md: {
                startTabIndex: 1,
                startModel: 1
            },
            lg: {},
            xl: {},
            xxl: {},
        },
    }
}

我正在尝试从我的模板中覆盖这些选项。

{% set configuratorOptions = {
    configurator: {
        startTabIndex: 1,
        startModel: 10,
        responsive: {
            xs: {},
            sm: {},
            md: {},
            lg: {},
            xl: {},
            xxl: {},
        },
    }
} %}

data-product-configurator-options="{{ configuratorOptions|json_encode }}

startTabIndex
startModel
工作正常,但我想清空响应设置,如上例所示。选项合并后,我的选项看起来像这样并且无法正常工作。我认为是因为类型从对象切换到数组。

{
    "configurator": {
        "startTabIndex": 1,
        "startModel": 10,
        "responsive": {
            "0": [],
            "576": [],
            "768": [],
            "992": [],
            "1200": [],
            "undefined": []
        }
    }
}

getSettings 函数不适用于此。如何防止类型切换?

当我用值覆盖响应式设置时它就起作用了

有效

{% set configuratorOptions = {
    configurator: {
        startTabIndex: 1,
        startModel: 10,
        responsive: {
            sm: {
                startTabIndex: 123,
                startModel: 123,
            },
        },
    }
} %}

但我无法清空它们。

不起作用

{% set configuratorOptions = {
    configurator: {
        startTabIndex: 1,
        startModel: 10,
        responsive: {
            sm: {},
        },
    }
} %}
twig shopware6
1个回答
0
投票

您可以通过将以下值

JSON_FORCE_OBJECT
传递给
flag
函数的
json_encode
参数来解决此问题

{% set configuratorOptions = {
    configurator: {
        startTabIndex: 1,
        startModel: 10,
        responsive: {
            xs: {},
            sm: {},
            md: {},
            lg: {},
            xl: {},
            xxl: {},
        },
    }
} %}


{{ configuratorOptions|json_encode(constant('JSON_FORCE_OBJECT'))|raw }}

演示


参考文献

© www.soinside.com 2019 - 2024. All rights reserved.