【问题标题】:Magento module - overridden a controller, adding templatesMagento 模块 - 覆盖控制器,添加模板
【发布时间】:2023-03-22 13:46:01
【问题描述】:

我目前正在开发一个 Magento 扩展,并且我已经覆盖了一个核心控制器,它工作正常。

我现在已经向我的控制器添加了一个新动作。问题是,每当我调用该操作时,都会生成一个空白页。如果我回显某些内容,它会正确显示。

因此,我深入研究了客户模块和控制器的核心。我在那里看到像indexAction() 这样的方法以这种方式实现布局:

<?php
public function indexAction()
{
  $this->loadLayout();
  $this->_initLayoutMessages('customer/session');
  $this->_initLayoutMessages('catalog/session');

  $this->getLayout()->getBlock('content')->append(
      $this->getLayout()->createBlock('customer/account_dashboard')
  );
  $this->getLayout()->getBlock('head')->setTitle($this->__('My Account'));
  $this->renderLayout();
}

我将其转移到我自己的操作中,现在布局已正确呈现。现在回答问题:

无论我在-&gt;createBlock('...') 调用中输入什么内容,都不会在内容区域中呈现任何内容。

如何指定我自己的块的位置以呈现为页面的内容,同时仍然用布局装饰它?

我尝试摆弄 /design/frontend/base/default/layout/myaddon.xml 中的 xml 文件,但无法真正发挥作用。

【问题讨论】:

    标签: php magento e-commerce


    【解决方案1】:

    在单个 StackOverflow 帖子中涵盖整个 Magento 布局系统有点多,但您应该能够通过以下内容实现您想要的。

        $block = $this->getLayout()->createBlock('Mage_Core_Block_Text');
        $block->setText('<h1>This is a Test</h1>');
        $this->getLayout()->getBlock('content')->append($block);
    

    从上面开始,您应该能够构建您需要的东西。这个想法是您正在创建自己的块,然后将它们附加到布局中的现有块。理想情况下,您创建自己的 块类 来实例化(而不是 Mage_Core_Block_Text),并使用其内部模板机制加载 phtml 文件(将 HTML 生成与代码生成分开)。

    如果您有兴趣了解布局系统如何工作的内部原理,那么您可能会比从我写的关于该主题的an article 开始做得更糟。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 2019-01-04
      • 2012-07-06
      • 1970-01-01
      相关资源
      最近更新 更多