【发布时间】:2016-03-08 11:40:08
【问题描述】:
我们可以使用 unsetBlock() 方法删除 Magento1 中的块,但在 Magento2 中它不起作用。那么,请帮助如何以编程方式删除 Magento2 中的块?
【问题讨论】:
标签: magento2
我们可以使用 unsetBlock() 方法删除 Magento1 中的块,但在 Magento2 中它不起作用。那么,请帮助如何以编程方式删除 Magento2 中的块?
【问题讨论】:
标签: magento2
使用unsetElement() 方法删除块。
就像
$layout = $this->getLayout();
$block = $layout->getBlock('catalog.topnav'); // block name
$layout->unsetElement('catalog.topnav');
【讨论】:
contactForm,但没有成功...
您需要尝试这种方式,例如,我正在从侧边栏中删除比较,因此我将 default.xml 覆盖为 app/design/frontend/Your_Theme/theme_name/Magento_Catalog/layout
<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="catalog.compare.sidebar" remove="true"/>
</body>
【讨论】:
要从页面中删除特定块,请打开您的自定义布局 xml 并将以下代码放在 body 标签下
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body><referenceBlock name="Your_Block" remove="true"/>
</body>
</page>
将 Your_Block 更改为您需要删除的块名称
【讨论】:
理想情况下,有不同的方法可以做到这一点。最好的方法是使用布局文件。
1) 如果您已经构建了一个模块,您可以在 app/code/Namespace/Your_Module/view/frontend/layout/frontname_controllername_controlleraction.xml 中创建一个 xml 文件的布局并添加以下代码
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceBlock name="block_name" remove="true" />
</page>
2) 如果您还没有创建自己的自定义模块,您可以简单地在 app/design/frontend/Custom_Theme/Theme_name/Module_Name/layout 中编写自定义 xml 并添加以下代码。
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceBlock name="block_name" remove="true" />
</page>
【讨论】: