Call magento template via $this->getChildHtml()

2019-08-28 20:42发布

问题:

I have a contact form template (modal.phtml), which I want to add to all pages on my site, but I need to be able to position it precisely, so I want to insert into the relevant page templates using $this->getChildHtml('contacts-modal'), instead of inserting via layout xml. So in page/2columns-right.phtml, I want to use that call to insert the template stored in contacts/modal.phtml. The layout xml i have below is automatically inserting this template - how do i go about correcting this? Thanks for any pointers, and apologies if this is a very basic thing!

       <reference name="content">
            <block type="core/template" name="contacts-modal" as="contacts-modal" template="contacts/modal.phtml"/>
        </reference>

回答1:

If you want to insert the template with $this->getChildHtml('contacts-modal') you have to declare it in the layout xml, look into the class Mage_Core_Block_Abstract to see how getChildHtml works. If you want to add it to other pages that don't have 2columns-right as main template, you have to add the xml reference like you did with 2columns-right, or you can use inside the parent template echo $this->getLayout()->createBlock('core/template')->setTemplate('contacts/modal.phtml')



标签: magento