【问题标题】:Magento: Several custom blocks all outputting the same code!Magento:几个自定义块都输出相同的代码!
【发布时间】:2011-01-08 23:27:49
【问题描述】:

我有一个自定义模块,它有几个块。如果我将这些块一个接一个地包含在 CMS 页面中,它们将按预期工作。如果我通过布局 XML 文件包含它们,它们都会显示 XML 中最后一个调用的源代码。接下来是一个最小的测试用例(对我来说就是表现出这种行为),以及预期的和实际的结果。

代码

/app/etc/modules/Test_Tester.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Test_Tester>
            <active>true</active>
            <codePool>local</codePool>
        </Test_Tester>
    </modules>
</config>

/app/code/local/Test/Tester/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Test_Tester>
            <version>0.1.0</version>
        </Test_Tester>
    </modules>
    <global>
        <blocks>
            <test_tester>
                <class>Test_Tester_Block</class>
            </test_tester>
        </blocks>
    </global>
</config>

/app/code/local/Test/Tester/Block/One.php

<?php
class Test_Tester_Block_One extends Mage_Catalog_Block_Product_List_Upsell
{
    protected function _prepareData()
    {
echo 'One.php';

//...MORE code here, it's not really relevant though
    }
}

/app/code/local/Test/Tester/Block/Two.php

<?php
class Test_Tester_Block_Two extends Mage_Catalog_Block_Product_List_Upsell
{
    protected function _prepareData()
    {
echo 'Two.php';

//...MORE code here, it's not really relevant though
    }
}

/app/design/frontend/INTERFACE/TEMPLATE/layout/page.xml(在

..
<block type="core/text_list" name="testa" as="testa" />
<block type="core/text_list" name="testb" as="testb" />
..

/app/design/frontend/INTERFACE/TEMPLATE/layout/cms.xml(在

<reference name="testa">
  <block type="test_tester/one" template="tester/one.phtml"/>
</reference>
<reference name="testb">
  <block type="test_tester/two" template="tester/two.phtml"/>
</reference>

/app/design/frontend/INTERFACE/TEMPLATE/template/page/home_template.phtml

<?php echo $this->getChildHtml('testa'); ?>
<?php echo $this->getChildHtml('testb'); ?>

/app/design/frontend/INTERFACE/TEMPLATE/tester/one.phtml

one.phtml

/app/design/frontend/INTERFACE/TEMPLATE/tester/two.phtml

two.phtml

预期

这应该打印出来(在主页上,其中包含块):

One.php
one.phtml
Two.php
two.phtml

实际输出

如果我在主页 CMS 页面中包含这些块,如下所示:

{{block type="test_tester/one" template="tester/one.phtml"}}
{{block type="test_tester/two" template="tester/two.phtml"}}

...我得到了预期的输出。但是,在代码示例中使用上述布局,我得到:

Two.php
two.phtml
Two.php
two.phtml

我觉得我疯了 - 我看不到我在搞砸的地方。

【问题讨论】:

    标签: magento


    【解决方案1】:

    尝试在主页 CMS 页面中为您的块命名。我收到的类似错误已通过这种方式解决。我看到您正在尝试将块包装在文本列表中,但据我所知,您从未真正从 CMS 页面中识别块。试试这个效果:

    {{block type="test_tester/one" template="tester/one.phtml" name="testa"}}
    {{block type="test_tester/two" template="tester/two.phtml" name="testb"}}
    

    如果您无法摆脱作为容器的父块,您可能需要重新考虑部分布局。如果这两种方式都不起作用,请告诉我,我们会尝试其他方法。希望对您有所帮助。

    谢谢, 乔

    【讨论】:

    • 有趣的是,这确实有效 - 它对 CMS {{block}} 样式包含没有影响(如您的示例中所示) - 这些在有或没有名称属性的情况下都有效。但是,它确实修复了 'cms.xml' 行。如果这些缺少名称,它们将无法正常工作。
    • 想一想,这是有道理的。使用内联语法 {{ }},块会立即输出,并且不需要名称。但是,使用 指定的块不会自动呈现,因此您需要一个句柄来呈现它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    相关资源
    最近更新 更多