通过 module.xml 文件进行 Magento 块注入
在我正在编写的模块中,我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您查看
page.xml
中内容块的声明,您将看到以下内容。默认情况下,名为 content 的块是
core/text_list
,它会转换为Mage_Core_Block_Text_List
。core/text_list
块的用途很简单。它们自动渲染插入其中的任何块。这就是为什么您可以成功地将块插入到内容中。您想要插入的块
是
catalog/product_view
,它会转换为Mage_Catalog_Block_Product_View
,它最终继承自Mage_Core_Block_Template
代码>.这使它成为一个模板块。模板块不会自动渲染其子元素。模板块将呈现一个phtml
文件。如果该phtml
包含对的调用,则将呈现具有该特定名称的块。如果
phtml
文件包含对所有子块的调用,则将呈现。
因此,当您说
要将
mymodule/folder_class
类型的块插入到product.info
块中时。但是,由于product.info
是模板块而不是文本列表块,因此它不会呈现您插入的块。您需要将自定义catalog/product/view.phtml
模板添加到您的主题(通过复制基本模板),然后添加到view.phtml
的底部, 添加If you look at the declaration of the content block up in
page.xml
, you'll see the following.By default, the block named content is a
core/text_list
, which translates to aMage_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
is a
catalog/product_view
, which translates to aMage_Catalog_Block_Product_View
, which ultimately inherits fromMage_Core_Block_Template
. That makes it a Template block. Template blocks do not automatically render their children. Template blocks will render aphtml
file. If thatphtml
contains a call tothen the block with that specific name will be rendered. If the
phtml
file contains a call tothen all the child blocks will be rendered.
So, when you say
you're inserting a block of type
mymodule/folder_class
into theproduct.info
block. But, becauseproduct.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 customcatalog/product/view.phtml
template to your theme (by copying the base template), and then at the bottom ofview.phtml
, add如果您想在
catalog/product/view操作
中添加该块,我相信您应该这样写,否则将catalog_product_view
替换为所需的操作或任何操作的默认值
。If you want to add the block in the
catalog/product/view action
, I believe you should put it like this, else replacecatalog_product_view
by the desired action ordefault
for any actions.