【问题标题】:rendering blocks and child blocks within a theme在主题中渲染块和子块
【发布时间】:2013-07-18 13:58:15
【问题描述】:

我目前正在尝试更好地了解块在 Magento 中的工作原理。我查看了一些文件以获得更好的想法,它有所帮助,但它们对于我目前有限的技能来说太复杂了,我仍然没有正确理解正在发生的事情以及如何将它们实施到我的网站中。我意识到它们对于与 Magento 合作是必不可少的,所以我想我会列出一个要尝试和实现的事情的清单:

  1. 显示一个块(完成)

  2. 显示块和子块

  3. 在 magento 布局中显示块

  4. 在 magento 布局的页面上放置一个块

  5. 了解最常用的“类型”属性以及何时使用它们

到目前为止,我已经整理好了 _index_index 命名空间/模块/etc/config.xml

<frontend>
    ....
    <layout>
        <updates>
            <learningblocks>
                <file>Namespace/Module/childblocks.xml</file>
                <file>Namespace/Module/blocks.xml</file>
            </learningblocks>
        </updates>
    </layout>
</frontend>

命名空间/模块/控制器/IndexController.php

class Namespace_Module_IndexController
    extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $this->loadLayout('learningblocks')->renderLayout();
    }

    public function blocksAction()
    {
        $this->loadLayout('blocknode')->renderLayout();
    }
}

前端/基础/默认/布局/命名空间/模块/blocks.xml

<layout>
    <blocknode>
        <block type="core/text" name="blocktest" output="toHtml" >
            <action method="setText">
                <args>some text to display on screen</args>
            </action>
        </block>
    </blocknode>
</layout>

以上内容按预期工作,并在白页上显示了字符串“要在屏幕上显示的一些文本”。但这就是我所能做的,我无法让子块渲染到屏幕上,也无法在主题中显示任何内容,更不用说尝试在该主题中移动它了

以下是我似乎无法开始工作的尝试之一。为什么这不起作用?

frontend/base/default/layout/namespace/module/childblocks.xml

<layout>
    <abcde>
        <block type="core/template" name="childblocks" output="toHtml" template="namespace/module/childblocks.phtml">
            <block type="core/text" name="anyname">
                <action method="setText">
                    <args>Some text to add to this page</args>
                </action>
            </block>
        </block>
    </abcde>

    <learningblocks_index_index>
        <update handle="abcde" />
    </learningblocks_index_index>
</layout>

frontend/base/default/template/namespace/module/childblocks.phtml

<p>from the childblock.phtml page</p><?php $this->getChildHtml(); ?>

注意:我已将命名空间和模块名称更改为更通用,希望更易于阅读(它们的名称选择得不是很好)。

【问题讨论】:

    标签: magento block


    【解决方案1】:

    我知道这不是一个完整的答案,但它可能会帮助那些遇到同样问题的人。我没有深入,因为我假设如果您在寻找答案时您已经阅读了THIS ARTICLE 并且涵盖了所有内容,我假设您遇到了与我相同的问题,即误解了您从中学到的知识教程。

    在阅读此答案时,请注意我对 Magento 非常陌生,此处可能存在一些不准确之处,如果我确信有人会纠正我并进行相应的编辑。

    首先这是错误的

    public function indexAction()
    {
        $this->loadLayout('learningblocks')->renderLayout();
    }
    

    应该是这个

    public function indexAction()
    {
        $this->loadLayout()->renderLayout();
    }
    

    然后您必须将布局 xml 中的 learningblocks 节点映射到该操作 module_controller_action。这样做会在您的主题内的页面中显示该块。

    所以要渲染一个子块

    在 layout.xml 中添加类似的内容

    <module_controller_action>
        <reference name="content">
            <block type="module/blockname" name="unique_name" output="toHtml" template="path/toyou/template.phtml" >  
                <block type="module/blockname" name="another_unique_name" output="toHtml" template="path/toyou/template.phtml" />
            </block>
        </reference>
    </module_controller_action>  
    

    然后在你的模板文件中回显

    $this->getChildHtml('another_unique_name')
    

    如果您想从页面中删除块,请使用删除节点,例如

    <remove name="right"/>
    <remove name="left"/>
    

    This page will offer a list of attributes that can be used 老实说,我发现浏览 magento 文件比那个页面更有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多