You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Magento 2中CMS块无法渲染页面空白问题排查

问题描述

我有一个标识为wib_home_page_content的CMS块,需要通过控制器和XML布局在前端渲染。目前已成功获取CMS块内容,但前端页面显示空白,无法正常展示内容。相关代码如下:

控制器代码(Brady/Wib/Controller/Index/Index.php)

<?php

namespace Brady\Wib\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Cms\Model\BlockRepository;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action
{
    protected $blockRepository;
    protected $resultPageFactory;

    public function __construct(
        Context $context,
        BlockRepository $blockRepository,
        PageFactory $resultPageFactory
    ) {
        $this->blockRepository = $blockRepository;
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }
    
    public function execute()
    {
        try {
            // Fetch the CMS block
            $block = $this->blockRepository->getById('wib_home_page_content'); // The block identifier
            $blockContent = $block->getContent();
    
            // Pass the content to the layout
            $resultPage = $this->resultPageFactory->create();
            $block = $resultPage->getLayout()->getBlock('Magento\Framework\View\Element\Template');
            if ($block) {
                $block->setData('my_data', $blockContent);
            }
            return $resultPage;
        } catch (\Exception $e) {
            // Handle errors (e.g., block not found)
            $this->messageManager->addErrorMessage(__('The CMS block could not be loaded.'));
            return $this->_redirect('noroute');
        }
    }
}

XML布局文件(Brady/Wib/view/frontend/layout/wib_index_index.xml)

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <!-- The CMS block content will be dynamically appended here -->
        </referenceContainer>
    </body>
</page>
问题原因
  • 布局无渲染块:XML布局仅引用了content容器,但未在其中添加任何负责输出内容的块元素,容器为空导致无内容显示。
  • 控制器获取块逻辑错误:代码中getBlock('Magento\Framework\View\Element\Template')试图获取一个未在布局中定义的通用模板块,实际返回null,导致设置的my_data数据无法传递到任何渲染载体。
修正方案

方案一:直接在布局调用CMS块(最简方案)

无需控制器额外处理内容,直接在布局中引用目标CMS块:
修改XML布局文件:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Cms\Block\Block" name="wib_home_content">
                <arguments>
                    <argument name="block_id" xsi:type="string">wib_home_page_content</argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>

简化控制器代码,仅返回页面:

public function execute()
{
    try {
        return $this->resultPageFactory->create();
    } catch (\Exception $e) {
        $this->messageManager->addErrorMessage(__('The CMS block could not be loaded.'));
        return $this->_redirect('noroute');
    }
}

方案二:自定义模板块传递内容(适合需额外处理内容场景)

  1. 更新XML布局,添加自定义模板块:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="wib_home_template" template="Brady_Wib::home.phtml"/>
        </referenceContainer>
    </body>
</page>
  1. 创建模板文件Brady/Wib/view/frontend/templates/home.phtml:
<?php /* @var $block \Magento\Framework\View\Element\Template */ ?>
<?= $block->getMyData() ?>
  1. 修改控制器中获取块的逻辑,指定布局中定义的块名称:
public function execute()
{
    try {
        $block = $this->blockRepository->getById('wib_home_page_content');
        $blockContent = $block->getContent();

        $resultPage = $this->resultPageFactory->create();
        // 获取布局中定义的自定义模板块
        $templateBlock = $resultPage->getLayout()->getBlock('wib_home_template');
        if ($templateBlock) {
            $templateBlock->setData('my_data', $blockContent);
        }
        return $resultPage;
    } catch (\Exception $e) {
        $this->messageManager->addErrorMessage(__('The CMS block could not be loaded.'));
        return $this->_redirect('noroute');
    }
}

内容的提问来源于stack exchange,提问作者Pavan Shetty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 00:42:03