在 Magento2 中以编程方式删除块
Remove Block Programmatically in Magento2
我们可以使用 unsetBlock()
方法在 Magento1 中删除块,但在 Magento2 中它不起作用。那么,请帮助如何以编程方式删除 Magento2 中的块?
使用unsetElement()
方法移除方块。
喜欢
$layout = $this->getLayout();
$block = $layout->getBlock('catalog.topnav'); // block name
$layout->unsetElement('catalog.topnav');
您需要尝试这种方式,例如,我正在从边栏中删除比较,因此我将 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>
我们可以使用 unsetBlock()
方法在 Magento1 中删除块,但在 Magento2 中它不起作用。那么,请帮助如何以编程方式删除 Magento2 中的块?
使用unsetElement()
方法移除方块。
喜欢
$layout = $this->getLayout();
$block = $layout->getBlock('catalog.topnav'); // block name
$layout->unsetElement('catalog.topnav');
您需要尝试这种方式,例如,我正在从边栏中删除比较,因此我将 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>