【问题标题】:How to place my custom block inside another block in Magento using layout xml?如何使用布局 xml 将我的自定义块放在 Magento 的另一个块中?
【发布时间】:2014-02-22 10:58:43
【问题描述】:

首先我想说的是,我在网上搜索了一整天,找不到我想要的东西。我也是新手,如有违规请见谅。

我正在尝试开发一个模块,它将视频和图像一起添加到产品页面。我被这个概念困住了:

如何将我的块插入现有的基本块?例如,在产品页面中,有一个块 product.info。在此块内有“可用性”、“价格”等。 如何使用模块的布局 xml 和模板在“可用性”下方和“价格”上方插入自定义块。

所以我正在尝试使用我的模块的布局文件来实现这样的目标:

 <catalog_product_view translate="label">
        <reference name="content">
            <reference name="product.info">
                WRITE BLOCK HERE SO THAT MY BLOCK SHOWS BELOW AVAILABLITY
            </reference>
        </reference>
    </catalog_product_view>

这可能吗?还是我必须重写核心类 Mage_Catalog_Block_Product_View 才能做到这一点?

PS:基本上我的目标是在图片旁边列出我的视频。现在,我可以从模块中列出我的视频,但在这种情况下不会出现图像。我用过

<block type="myblock/myblock" name="somename" as="media" template="abc.phtml"/>

所以我想将我的块附加到现有内容。

【问题讨论】:

    标签: magento


    【解决方案1】:

    我解决了。我不得不重写 Mage_Catalog_Block_Product_View_Media 。

    在我的课堂上,我像这样重写了函数 _toHtml 函数:

    public function _toHtml()
    {
        $html = parent::_toHtml();
        $html.=$this->getChildHtml('media_video');
        return $html;
    }
    

    “media_video”是我的块。我的布局 xml 文件:

    <catalog_product_view translate="label">
        <reference name="content">
            <reference name="product.info">
                <reference name="product.info.media">
                <block type="myblock/myblock" name="somename" as="media_video" template="beta/abc.phtml"
                       before="-"/>
                </reference>
            </reference>
        </reference>
    </catalog_product_view>
    

    【讨论】:

      【解决方案2】:

      您可以添加新块而不是直接添加现有块。

       <catalog_product_view translate="label">
          <reference name="content">
              <reference name="product.info">
          <block type="myblock/myblock" name="somename" as="media_new" template="abc.phtml"/>
              </reference>
          </reference>
      </catalog_product_view>
      

      使用 phtml 文件中的以下代码获取新块

      <?php echo $this->getChildHtml('media_new') ?>
      

      谢谢

      【讨论】:

      • 我应该把 getChildHtml 放在哪里?在我的模板中还是在主题的模板文件中?因为我正在尝试创建第三方模块,所以我不应该编辑任何现有文件。
      【解决方案3】:

      请注意以下代码中的output="toHtml"。这将在product.info 部分打印您的块。

      <catalog_product_view translate="label">
          <reference name="content">
              <reference name="product.info">
                  <block type="myblock/myblock" name="somename" as="media"
                      template="abc.phtml" output="toHtml" before="-" />
              </reference>
          </reference>
      </catalog_product_view>
      

      【讨论】:

        猜你喜欢
        • 2010-11-09
        • 1970-01-01
        • 2014-10-12
        • 2015-02-23
        • 2017-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多