dtqi87613 2013-11-06 13:09 采纳率: 0%
浏览 99
已采纳

如何在Magento的其他目录/页面中访问$ this-> getChildHtml()等方法?

All I want to do is move a search box. This search box is currently displayed in the header, directly next to the logo, and is generated by the following code:

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

in /app/design/frontend/MYTHEME/default/template/page/html/header.phtml

I would like this search box inline with the navigation links, which are located in top.phtml which is located in another directory. But when I use the code

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

the search box does not appear. I understand that this is because the meaning of $this has changed, but what I don't understand is how to display the search box? What do I replace $this with?

I tried replacing it with Mage_Page_Block_Html_Header since that was the definition of $this in header.phtml, but to no avail. Can anybody point me in the right direction, or provide an explanation as to how I access methods once the definition of $this has changed?

  • 写回答

1条回答 默认 最新

  • dongnaosuan5407 2013-11-06 13:52
    关注

    You need to make layout update and include block topSearch into block containing top.phtml. Look into app/design/frontend/.../layouts/...xml files, find how topSearch is declared and then find where the block using top.phtml template is declared. Then move topSearch block as a child of top block. I mean add layout xml update like this:

    <default>
        <reference name="catalog.topnav">
            <block type="core/template" name="top.search" as="topSearch" template="catalogsearch/form.mini.phtml"/>
        </reference>
    </default>
    

    Another solution is try next in your template:

    echo $this->getLayout()->getBlock('top.search')->toHtml()
    

    If it would not work then find in layouts topSearch block and try to use in code above name of block instead of alias.

    You can read more about Magento view level here: http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/0_-_theming_in_magento/designing-for-magento

    Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?