【问题标题】:Magento - xml layouts, specify value for ifconfig?Magento - xml布局,为ifconfig指定值?
【发布时间】:2011-04-08 13:58:35
【问题描述】:

我确定我之前在某个地方看到过,为 xml ifconfig 语句指定了一个值(默认为布尔值)。无论如何,在管理员中禁用模块实际上不起作用(仅禁用模块输出)。 但是您可以在布局文件中添加 ifconfig,例如,仅当模块禁用时设置模板如下:

<action method="setTemplate" ifconfig="advanced/modules_disable_output/Myname_Mymodule">
    <template>mytemplate.phtml</template>
</action>

那么你怎么能颠倒这个,所以只有当模块启用时才设置模板?比如:

<action method="setTemplate" ifconfig="advanced/modules_disable_output/Myname_Mymodule" value="0">
    <template>mytemplate.phtml</template>
</action>

【问题讨论】:

    标签: magento


    【解决方案1】:

    这与我一直在努力的something(自链接)非常吻合。

    如果不重写类来改变ifconfig 的行为,你就不能完全按照你的意愿去做。这是实现ifconfig 功能的代码。

    File: app/code/core/Mage/Core/Model/Layout.php
    protected function _generateAction($node, $parent)
    {
        if (isset($node['ifconfig']) && ($configPath = (string)$node['ifconfig'])) {
            if (!Mage::getStoreConfigFlag($configPath)) {
                return $this;
            }
        }
    

    如果检测到存在 ifconfig 并且配置值返回 true,则不会调用 action 方法。你可以重写_generateAction 并实现你自己的条件,但是维护重写的标准负担落在了你身上。

    更好的方法是在您的操作参数中使用辅助方法。像这样的

    <action method="setTemplate">
        <template helper="mymodule/myhelper/switchTemplateIf"/>
    </action>
    

    将使用调用结果调用 setTemplate

    Mage::helper('mymodule/myhelper')->switchTemplateIf();
    

    switchTemplateIf 中实现您的自定义逻辑,既可以保留模板也可以更改模板,一切顺利。

    【讨论】:

    • 我只想检查 layout.xml 以检查字段的值,这是两个可能的值 -> 顶部或 -> 下面。因此,检查是否选择了顶部,然后在目录页面的 product_list 下方显示我的产品顶部 product_list ..thanx ..
    • 我们可以在自定义逻辑返回真或假的情况下调用辅助方法而不是 ifConfig。
    • 嗨艾伦,我们可以使用这种方法通过使用我们自己的模块的系统变量来设置某个Magento页面的模板布局吗?我为此提出了一个新问题。如果你能在这里检查一下会很高兴stackoverflow.com/questions/11593340/…
    • 谢谢!你节省了我的时间
    【解决方案2】:

    您可以只使用模块的 system.xml 来创建单独的 enable 设置。

    <config>
        <sections>
            <advanced>
                <groups>
                    <YOURMODULE>
                        <fields>
                            <enable>
                                <label>YOUR MODULE</label>
                                <frontend_type>select</frontend_type>
                                <source_model>adminhtml/system_config_source_enabledisable</source_model>
                                <show_in_default>1</show_in_default>
                                <show_in_website>1</show_in_website>
                                <show_in_store>1</show_in_store>
                            </enable>
                        </fields>
                    </YOURMODULE>
                </groups>
            </advanced>
        </sections>
    </config>
    

    然后使用布局文件中的新设置:

    <action method="setTemplate" ifconfig="advanced/YOURMODULE/enable">
        <template>mytemplate.phtml</template>
    </action>
    

    【讨论】:

    • 是的,这就是我目前正在做的事情,它确实有效,但使用默认模块禁用器会很好,而且我不必将它添加到每个布局中.
    • 如何检查该字段的值.. => ifvalue="...."... 我想检查一个字段的值,然后决定在顶部显示我的网格或product_list 的下方...谢谢...
    • @ime,目前在布局系统中没有ifvalue,只有ifconfig 用于检查非零值。您可以将其与一对是/否设置一起使用,一个用于“高于产品列表”,一个用于“低于产品列表”。 (这将允许用户同时打开或关闭您可能不想做的事情。)唯一的另一种方法是自己编写 PHP。
    • 嗯,没关系,但如果在辅助方法中编写代码逻辑并返回真或假值,那么我可以从布局 xml 中调用该辅助方法,如 ifconfig。 ?返回真或假......
    • 查看 Alan 的回答,了解使用助手的示例。如果您还有其他问题,请打开一个新问题。
    猜你喜欢
    • 2018-04-19
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    相关资源
    最近更新 更多