【问题标题】:Creating new block for getChildHtml() - Magento为 getChildHtml() 创建新块 - Magento
【发布时间】:2014-07-01 13:41:16
【问题描述】:

我正在尝试将 updateinfo 块添加到客户页面。

我把这段代码放在customer.xml布局中

<block type="core/text_list" name="updateinfo" as="updateinfo"/>
<reference name="updateinfo">
<block type="core/template" name="updateinfoBlock" template="customer/update_info.phtml" />
</reference>

并将update_info.phtml 添加到客户文件夹中。

还添加了 &lt;?php echo $this-&gt;getChildHtml('updateinfo') ?&gt; 到客户页面 但我仍然没有得到任何东西。卡住了。

【问题讨论】:

  • 你能发布你的update_info.phtml代码吗?
  • 这只是一个简单的 div,在

    中包含一些随机文本,以便在我配置它出现之前开始。它没有出现在模板提示路径中,所以我假设它没有加载到页面中

  • 你能发布你的 customer.xml 文件吗

标签: php xml magento block


【解决方案1】:

试试下面的代码

<block type="core/text_list" name="updateinfo" as="updateinfo">  
   <block type="core/template" name="updateinfoBlock" template="customer/update_info.phtml" /> 
</block>

要到达这个街区,你可以像这样

<?php echo $this->getChildHtml('updateinfo') ?>

【讨论】:

    【解决方案2】:

    试试:

    getChildHtml('updateinfoBlock') ?> 
    

    代替:

    getChildHtml('updateinfo') ?&gt;

    【讨论】:

    • 还是什么都没有,甚至试图把它放在 page.xml 中,但什么也没有。附言我每次都在刷新 Magento 缓存
    • 整个代码应该是这样的:&lt;?php echo getChildHtml('updateinfoBlock') ?&gt; 你也可以尝试将你的xml放入page.xmllocal.xml来确定。
    • 我知道它应该是这样的,这就是我所说的
    • 我设法通过它而无需在 xml 中使用 getLayout()->createBlock('core/template')->setTemplate('customer/ update_info.phtml')->toHtml();?>
    • Danijel,创建内联块肯定有效,但最好对 local.xml 进行更改并使用块更新和句柄更容易找到并防止以后出现问题。除非您有一个使用“updateinfo”引用名称的自定义模块,否则您的布局更新格式将不起作用
    【解决方案3】:

    如果你想使用getChildHtml(),你想设置你的块的as属性和name属性。 Name 属性一般用于布局文件本身的引用。如果需要通过模板文件调用块,则使用as 属性中指定的值。所以你需要像这样的块定义

    <block type="core/template" name="updateinfoBlock" as="update.info.block" template="customer/update_info.phtml" />
    

    你需要将此块称为

     <?php $this->getChildHtml('update.info.block'); ?> 
    

    但是core/template 块的父块是core/text_list 类型。该类型块内的子级将自动呈现。意味着您不需要通过getChildHtml() 方法调用这些孩子。一些例子是leftrightcontent 块。里面的方块会自动渲染。

    因此,将core/template 类型的块直接放在布局中您需要的任何位置,然后如上所示调用它。你不需要像以前那样把它放在另一个块中,除非它是必要的。

    【讨论】:

      猜你喜欢
      • 2014-06-23
      • 1970-01-01
      • 2015-06-03
      • 2020-11-26
      • 1970-01-01
      • 2012-07-18
      • 2013-10-29
      • 2011-05-21
      • 1970-01-01
      相关资源
      最近更新 更多