【问题标题】:How to show the static blocks in Magento 2?如何在 Magento 2 中显示静态块?
【发布时间】:2015-12-13 18:59:00
【问题描述】:

我正在创建一个 magento 2 主题。 我想在 cms 主页上显示自定义块。

我想知道在phtml和xml布局中显示静态块,cms页面内容

我该怎么办?

【问题讨论】:

  • 我想知道在phtml和xml布局中显示静态块,cms页面内容。

标签: php magento2


【解决方案1】:

请尝试使用以下代码。

在 Phtml 文件中:

<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>

在 CMS 内容中:

{{block class="Magento\\Cms\\Block\\Block" block_id="block_identifier"}}

在 XML 文件中:

<referenceContainer name="content">
    <block class="Magento\Cms\Block\Block" name="block_identifier">
        <arguments>
            <argument name="block_id" xsi:type="string">block_identifier</argument>
        </arguments>
    </block>
</referenceContainer>

【讨论】:

  • 使用 Magento 2.0.2。出于测试目的,我将上面提供的 xml 插入到产品页面“自定义布局更新”中,并在 app/design/frontend/Magento/&lt;Vendor&gt;/&lt;theme&gt;/Magento_Catalog/templates/product/view/details.phtm 中提供了 php 块。我仍然无法看到这个块。我做错什么了吗?
  • 我还需要像这样设置其他参数&lt;?php echo $this-&gt;getLayout()-&gt;createBlock('vendor\module\Block\BlockClass')-&gt;setName('name')-&gt;setTemplate('vendor_module::template.phtml')-&gt;toHtml(); ?&gt;
【解决方案2】:

上面是正确的,另外,主页的xml文件是cms_index_index.xmlMagento_Cms文件夹内(这应该放在你自己的包/主题文件夹中,而不是在Magento/blankapp/code中) .

只需将上述 xml 代码放在该文件的 &lt;body&gt; 标记内,根据缓存设置,您可能需要清除它们才能看到调整。

【讨论】:

    【解决方案3】:

    这些都不适用于通过 default.xml 放置静态块。请提供适当的示例。在 2.0.7 中,当我在管理中创建名为 socialimg / socialimg 的静态块作为块标识符后,应用以下内容时,什么都没有显示。甚至不在记忆中。甚至没有搜索 exception.log 或 system.log

        <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceContainer name="header.panel">
                <block class="Magento\Framework\View\Element\Html\Links" name="header.links">
                    <arguments>
                        <argument name="css_class" xsi:type="string">header links</argument>
                    </arguments>
                </block>
            </referenceContainer>
    <referenceBlock name="socialimg1">
        <block class="Magento\Cms\Block\Block" name="socialimg">
            <arguments>
              <argument name="block_id" xsi:type="string">socialimg</argument>
            </arguments>
        </block>
    </referenceBlock>
            <referenceBlock name="logo">
                <arguments>
                    <argument name="logo_img_width" xsi:type="number">148</argument>
                    <argument name="logo_img_height" xsi:type="number">43</argument>
                </arguments>
            </referenceBlock>
            <referenceContainer name="footer">
                <block class="Magento\Store\Block\Switcher" name="store_switcher" as="store_switcher" after="footer_links" template="switch/stores.phtml"/>
            </referenceContainer>
            <referenceBlock name="report.bugs" remove="true"/>
            <move element="copyright" destination="before.body.end"/>
        </body>
    </page>
    

    【讨论】:

      【解决方案4】:

      使用以下代码在CMS页面中显示静态块-

      {{block class="Magento\\Cms\\Block\\Block" block_id="block_identifier"}}
      

      使用以下代码在phtml文件中显示静态块-

      <?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-19
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多