有条件地在布局xml中显示/隐藏块
题
如何在Magento的布局XML中有条件地添加一个块(取决于管理面板中的配置)?
我们可以在操作上检查配置是否为true。在下面的示例中,如果 sample/config/show_toplinks
从管理面板(在系统 - >配置中)的配置是 真的, ,然后是模板文件 links.phtml
将用于渲染顶级链接。如果 sample/config/show_toplinks
是 错误的, ,然后将使用默认模板。
<reference name="top.links">
<action method="setTemplate" ifconfig="sample/config/show_toplinks">
<template>page/template/links.phtml</template>
</action>
</reference>
我在网络中的某个地方发现了这种解决方法。我们可以将一个空模板设置为顶部链接的默认模板,例如:
<reference name="top.links">
<action method="setTemplate" ifconfig="sample/config/show_toplinks">
<template>page/template/links.phtml</template>
</action>
<!-- OR set completely empty template -->
<action method="setTemplate">
<template>page/template/empty_template_for_links.phtml</template>
</action>
</reference>
在这种情况下,如果 sample/config/show_toplinks
是 真的, ,然后是模板 links.phtml
将使用并显示顶部链接。但是如果 sample/config/show_toplinks
是 错误的, ,然后 empty_template_for_links.phtml
将使用模板,该模板是完全空的,因此它不会返回任何HTML,并且顶部链接将看不到。
- 是否有其他方法可以根据管理面板中的配置有条件地显示或隐藏块?
- 这个解决方法安全吗?
- 这会导致任何意外错误吗?
编辑:
根据所有答案,我认为Rick Kuipers的解决方案看起来最方便我的情况。但是我还有另一个相关的问题:
<block type="core/template" name="my_block" template="my/block.phtml" />
<!-- ...add more blocks here -->
<reference name="footer">
<action method="append" ifconfig="sample/config/show_toplinks">
<block>my_block</block>
</action>
<!-- ...append more blocks here -->
</reference>
如果我有很多这样添加的块(使用 append
方法和 ifconfig
),假设50,它会影响性能吗?只有一些块才能真正显示(这取决于系统中的用户设置 - > config),但是我需要在有条件地将它们内部附加到内部之前添加所有这些块 <reference name="footer">...</reference>
.
Magento是否会立即处理所有添加的块?
<block type="core/template" name="my_block" template="my/block.phtml" />
或仅当必须最终显示在模板中时,才能处理块?那么,尽管只需要显示其中的一些块,但Magento还是必须处理我所有的50个块?
解决方案
我想添加我的选择,而不是Benmarks的答案。
我的方法是使用附加操作:
<block type="core/template" name="my_block" template="my/block.phtml" />
<reference name="head">
<action method="append" ifconfig="myblock/general/enabled"><block>my_block</block></action>
</reference>
其他提示
通过使用 _template
隐藏输出的属性是一种新颖的方法。我希望在配置选项上反向值,以便是= 0(也许是自定义源模型)并调用 unsetChild
在父母身上 头 堵塞:
<reference name="head">
<action method="unsetChild" ifconfig="sample/config/show_toplinks">
<child>topLinks</child>
</action>
</reference>
关于您的问题:
我的方法只是扩展了你的
我看不出为什么不会
同样,您的代码在方法背后非常安全,不会引起例外(
getStoreConfig
对于一个人来说,只会返回虚假的值,因此不会添加条件句柄),但是如果不存在空模板文件,您将获得异常。使用自关闭标签传递空值(例如<template />
)
如果我正在开发此功能,我将扩展您的解决方案,以包括一个观察者,该观察者检查配置并有条件地在您的布局中添加句柄。然后,在布局文件中,您可以在不同的句柄中设置两个操作 -default
和 show_toplinks
<config>
<global>
<!-- stuff -->
<events>
<controller_action_layout_load_before>
<observers>
<my_module_add_handle>
<class>my_module/Observer</class>
<method>addHandle</method>
</my_module_add_handle>
</observers>
</controller_action_layout_load_before>
</events>
<!-- other stuff -->
</global>
</config>
然后在你的 Observer
模型...
public function addHandle(Varien_Event_Observer $observer)
{
if (Mage::getStoreConfig('sample/config/toplinks') {
$observer->getEvent()->getLayout()->getUpdate()
->addHandle('show_toplinks');
}
}
aaaand终于在您的布局中:
<default>
<reference name="top.links">
<!-- yup -->
</reference>
</default>
<show_toplinks>
<reference name="top.links">
<!-- another yup -->
</reference>
</show_toplinks>