0

I am using Magento 1.7. I am not sure of how getChildHtml() works. I have a piece of code in header.phtml,

echo $this->getChildHtml('topSearch');

used for showing the search box in my page. But I am not sure of which .phtml is using. First I thought it was using form.mini.phtml under app/design/frontend/default/custom_theme/template/catalogsearch/form.mini.phtml, but after enabling path hints in the Admin, it seems it's using another one.

I see that getChildHtml() is used a bit everywhere, and I see the idea of what it is used for, but I don't understand how it exactly works.

I need to know how can I define the string passed as parameter to getChildHtml(), and if there is an associated .phtml. Any help is much appreciated.

  • Ok. So getChildHtml() takes name or alias(if present) of a block. We use it to call a child block from inside of a parent block. It mostly gets defined in layout xml files. – Kingshuk Deb Apr 6 '17 at 11:38
  • 1
    Example go to app\design\frontend\base\default\template\catalog\product\view.phtml open it. Check $this->getChildHtml('addto') line. Now go to catalog.xml inside app\design\frontend\base\default\layout and search for it. You will find this line <block type="catalog/product_view" name="product.info.addto" as="addto" template="catalog/product/view/addto.phtml"/> which is a child block to <block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml"> this parent block. – Kingshuk Deb Apr 6 '17 at 11:43
  • Thank you! You can add an answer with the explanation and I'll accept it if you would like to – LeCodeur Apr 7 '17 at 10:28
1

getChildHtml() takes name or alias(if present) of a block. We use it to call a child block from inside of a parent block. It mostly gets defined in layout xml files.

Example go to app\design\frontend\base\default\template\catalog\product\vi‌​ew.phtml open it. Check $this->getChildHtml('addto') line. Now go to catalog.xml inside app\design\frontend\base\default\layout and search for it. You will find this line <block type="catalog/product_view" name="product.info.addto" as="addto" template="catalog/product/view/addto.phtml"/> which is a child block to <block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml"> this parent block.

1

getChildHtml is the magento function defined under app/code/core/Mage/Core/Block/Abstract.php

The phtml files are set dynamically from xml like this in catalogsearch.xml

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

and then these are loaded by alias name (defined here as="topSearch") Magento to the frontend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.