在布局xml中设置Magento块模板 [英] Set Magento block template in layout xml

查看:81
本文介绍了在布局xml中设置Magento块模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Magento的布局xml中设置块模板时遇到麻烦.我试图设置一个子块的模板,而不是整个页面的布局(几乎所有文档都解释了如何设置布局的模板).

Having trouble setting a block template in Magento's layout xml. I'm attempting to set the template of a child block, not the entire page layout (almost all docs out there explain how to set template of the layout).

背景:我正在使用自定义操作中的更新布局手柄,使用模块布局xml中的<update />标记.

Background: I'm updating a layout handle in my custom action, using the <update /> tag in my module's layout xml.

本质上,我想重用内置产品视图操作的布局和块,但为几个块提供自定义模板. (不仅仅是覆盖,这些覆盖还需要是全新的模板,这些模板仅在我的自定义操作中触发,并且它们本身是可覆盖的.)

Essentially, I want to reuse the layout and blocks of the built in product view action, but provide custom templates for a few blocks. (Not just overrides, these need to be brand new templates that are only triggered on my custom action and are themselves overrideable).

我的布局html:

<?xml version="1.0"?>
<layout version="0.1.0">
<mymodule_product_index>
    <update handle="catalog_product_view" />
    <reference name="content">
        <block type="catalog/product_view" 
        name="product.info" output="toHtml" template="mymodule/product.phtml" />
        </reference>

    <reference name="product.info.bundle">
        <action method="setTemplate"><template>mymodule/customtemplate.phtml</template></action>
    </reference>
</mymodule_product_index>
</layout>

product.info.bundle上的setTemplate永不起作用;它似乎根本不影响布局.我试过将<reference>包裹在来自父块的其他<reference>节点中,没有任何效果.是否可以通过这种方式替换块模板?我觉得我的问题源于我正在使用<update />.

The setTemplate on product.info.bundle never works; it doesn't seem to affect layout at all. I've tried wrapping the <reference> in other <reference> nodes from parent blocks with no effect. Is it possible to replace block templates in this way? I feel that my problem stems from the fact I'm using an <update />.

顺便说一句,我知道我的布局xml正在加载并且没有错误,文件的其余部分工作正常,已禁用缓存,无论如何都清除了缓存,等等.

By the way, I know my layout xml is being loaded and there are no errors, the rest of the file is working fine, caching is disabled, have cleared cache anyway, etc.

推荐答案

您的方法几乎是正确的.
两件事:
1.设置新模板而不是实例化新块
您将创建一个具有相同名称的新实例,替换原始实例,然后在其上设置新模板,而不仅仅是为product.info块分配一个不同的模板.而是使用此:

Your approach is almost correct.
Two things:
1. Set a new template instead of instantiating a new block
Instead of just assigning a different template to the product.info block, you are creating a new instance with the same name, replacing the original instance, and then the new template is set on that. Instead use this:

<mymodule_product_index>
    <update handle="catalog_product_view" />
    <reference name="product.info">
        <action method="setTemplate">
            <template>mymodule/product.phtml</template>
        </action>
    </reference>
</mymodule_product_index>

那应该以干净的方式处理产品视图模板.

That should take care of the product view template in a clean way.

2.处理处理订单
如果查看捆绑产品的视图块product.info.bundle的声明位置,您将在bundle.xml文件的名为<PRODUCT_TYPE_bundle>的布局更新句柄中看到它的发生.

2. Handle processing order
If you look at where the view block product.info.bundle for the bundled products is declared, you will see it happens in the bundle.xml file, in a layout update handle called <PRODUCT_TYPE_bundle>.

您的代码正在引用<[route]_[controller]_[action]>布局句柄(即<mymodule_product_index>)中的块.

Your code is referencing the block from the <[route]_[controller]_[action]> layout handle, i.e. <mymodule_product_index>.

这里要注意的是布局句柄的处理顺序. 大致是:

The thing to be aware of here is the processing order of layout handles. Roughly it is:

  1. <default>
  2. <[route]_[controller]_[action]>
  3. <custom_handles>
  1. <default>
  2. <[route]_[controller]_[action]>
  3. <custom_handles>

<PRODUCT_TYPE_bundle>手柄属于布局手柄的第三种类型,这意味着它是在<mymodule_product_index>手柄之后进行处理的. 本质上,您是在声明块之前引用product.info.bundle.

The <PRODUCT_TYPE_bundle> handle belongs to the third type of layout handles, which means it is processed after the <mymodule_product_index> handle.
In essence, you are referencing the block product.info.bundle before it has been declared.

要解决此问题,您还需要使用<PRODUCT_TYPE_bundle>手柄.当然,这将影响每个捆绑产品的展示.仅使用布局XML并没有解决之道.

To fix this you will need to use the <PRODUCT_TYPE_bundle> handle as well. Of course this will effect every bundled product display. Using layout XML only there is no clean way around that.

以下是一些解决该问题的建议.

Here are a few suggestions how to solve that problem.

您可以在模块中创建一条单独的路线来显示捆绑的产品,然后使用该页面的update指令包含<PRODUCT_TYPE_bundle>句柄.

You could create a separate route in your module to show the bundled products, and then include the <PRODUCT_TYPE_bundle> handle using an update directive for that page, too.

在您的自定义动作控制器中,您可以添加在之后 <PRODUCT_TYPE_bundle>之后处理的另一个布局更新句柄.

In your custom action controller, you could add another layout update handle that is processed after <PRODUCT_TYPE_bundle>.

如果实例化了模板,可以使用事件观察器在product.info.bundle块上设置模板.一种可能是事件controller_action_layout_generate_blocks_after.

You could use an event observer to set the template on the product.info.bundle block if it is instantiated. One possibility would be the event controller_action_layout_generate_blocks_after.

您明白了,有很多方法可以解决此问题,但是它们需要PHP.

You get the idea, there are many ways to work around this, but they require PHP.

这篇关于在布局xml中设置Magento块模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆