通过 module.xml 文件进行 Magento 块注入

发布于 2024-10-11 04:36:40 字数 1618 浏览 1 评论 0原文

在我正在编写的模块中,我想使用 mymodule.xml 在该块之后插入我自己的块:

<block type="catalog/product_view_media" name="product.info.media" as="media" template="catalog/product/view/media.phtml"/>

它嵌套

<reference name="content">
    <block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">

在 app/design/frontend/base/default/layout/catalog.xml 中,

我已经尝试过许多变体,例如:

<reference name="content">
   <reference name="product.info">
      <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
   </reference>
</reference>

和 只是

<reference name="product.info">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

<reference name="product.info">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

但我似乎无法找到正确的组合。 渲染我的块的唯一一个是:

<reference name="content">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

但显然它没有放置在我想要放置的位置。

In a module I am writing I want to use using mymodule.xml to insert my own block after this block:

<block type="catalog/product_view_media" name="product.info.media" as="media" template="catalog/product/view/media.phtml"/>

which is nested within

<reference name="content">
    <block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">

as can be seen in app/design/frontend/base/default/layout/catalog.xml

I have tried many variants such as:

<reference name="content">
   <reference name="product.info">
      <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
   </reference>
</reference>

and just

<reference name="product.info">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

and

<reference name="product.info">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

but I cant seem to find the correct combination.
the only one that renders my block is:

<reference name="content">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

but obviously it is not placed where I wanted it to be placed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清君侧 2024-10-18 04:36:40

如果您查看 page.xml 中内容块的声明,您将看到以下内容。

<block type="core/text_list" name="content" as="content" translate="label">

默认情况下,名为 content 的块是 core/text_list,它会转换为 Mage_Core_Block_Text_List

core/text_list 块的用途很简单。它们自动渲染插入其中的任何块。这就是为什么您可以成功地将块插入到内容中。

您想要插入的块

<block type="catalog/product_view" name="product.info" template="mymodule/folder/class.phtml" ...

catalog/product_view,它会转换为Mage_Catalog_Block_Product_View,它最终继承自Mage_Core_Block_Template代码>.这使它成为一个模板块。模板块不会自动渲染其子元素。模板块将呈现一个 phtml 文件。如果该 phtml 包含对的调用

$this->getChildHtml('block_name');

,则将呈现具有该特定名称的块。如果phtml文件包含对

$this->getChildHtml(); //no arguments

所有子块的调用,则将呈现。

因此,当您说

<reference name="product.info">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

要将 mymodule/folder_class 类型的块插入product.info 块中时。但是,由于 product.info 是模板块而不是文本列表块,因此它不会呈现您插入的块。您需要将自定义 catalog/product/view.phtml 模板添加到您的主题(通过复制基本模板),然后添加到 view.phtml 的底部, 添加

<?php echo $this->getChildHtml('mymodule.folder.class');?>

If you look at the declaration of the content block up in page.xml, you'll see the following.

<block type="core/text_list" name="content" as="content" translate="label">

By default, the block named content is a core/text_list, which translates to a Mage_Core_Block_Text_List.

The purpose of a core/text_list blocks is simple. They automatically render any blocks inserted into them. That's why you can insert a block into content successfully.

The block you want to insert into

<block type="catalog/product_view" name="product.info" template="mymodule/folder/class.phtml" ...

is a catalog/product_view, which translates to a Mage_Catalog_Block_Product_View, which ultimately inherits from Mage_Core_Block_Template. That makes it a Template block. Template blocks do not automatically render their children. Template blocks will render a phtml file. If that phtml contains a call to

$this->getChildHtml('block_name');

then the block with that specific name will be rendered. If the phtml file contains a call to

$this->getChildHtml(); //no arguments

then all the child blocks will be rendered.

So, when you say

<reference name="product.info">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

you're inserting a block of type mymodule/folder_class into the product.info block. But, because product.info is a template block and not a text list block, it doesn't render the block you inserted. You'll need to add a custom catalog/product/view.phtml template to your theme (by copying the base template), and then at the bottom of view.phtml, add

<?php echo $this->getChildHtml('mymodule.folder.class');?>
我的痛♀有谁懂 2024-10-18 04:36:40

如果您想在catalog/product/view操作中添加该块,我相信您应该这样写,否则将catalog_product_view替换为所需的操作或任何操作的默认值

<layout version="0.1.0">
    <catalog_product_view>
        <reference name="content">
            <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class">
            </block>
        </reference>
    </catalog_product_view>
</layout>

If you want to add the block in the catalog/product/view action, I believe you should put it like this, else replace catalog_product_view by the desired action or default for any actions.

<layout version="0.1.0">
    <catalog_product_view>
        <reference name="content">
            <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class">
            </block>
        </reference>
    </catalog_product_view>
</layout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文