Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Problem with $this->getChildHtml()
 
IanRyan
Sr. Member
 
Total Posts:  149
Joined:  2011-01-21
 

I have customised header.phtml as follows:-

<div class="page-header-container">
<div class="page-header">
<div class="logo-container">
<a class="logo-alt" title="<?php echo $this->getLogoAlt() ?>” href="<?php echo $this->getUrl('') ?>"><img class="logo" src="<?php echo $this->getLogoSrc() ?>” alt="<?php echo $this->getLogoAlt() ?>” />
</a>
</div>
<?php echo $this->getChildHtml('mainNavBar') ?>
<?php echo $this->getChildHtml('welcomeBar') ?>
</div>
</div>

header.html calls child welcomeBar.phtml. welcomeBar.phtml contains the following:-

<div class="welcome-bar">
<div class="welcome-msg"><?php echo $this->getWelcome() ?>
</div>
<div class="quick-links-container">
<a class="quick-links" title="My Account” href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK) ?>customer/account/login">My Account
</a> | 
<a class="quick-links" title="My Wishlist” href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK) ?>wishlist">My Wishlist
</a> | 
<a class="quick-links" title="My Cart” href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK) ?>checkout/cart">My Cart
</a> | 
<a class="quick-links" title="Checkout" href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK) ?>checkout">Checkout
</a> | 
<a class="quick-links" title="Log In” href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK) ?>customer/account/login">Log In
</a>
</div>
</div>

My problem is that the statement <div class="welcome-msg"><?php echo $this->getWelcome() ?> in welcomeBar.phtml does not return the configured welcome statement whereas if I add the same statement to header.phtml it does return the configured welcome statement.

What am I doing wrong?

 
Magento Community Magento Community
Magento Community
Magento Community
 
brobie
Sr. Member
 
Avatar
Total Posts:  139
Joined:  2008-07-28
 

We would need to see the layout xml files to be sure (and the code inside the block class), but whichever block is serving the template “welcomeBar.phtml” either does not have the “getWelcome()” function defined, or the $_data object array does not have a key of “welcome” inside of it.  This ALL has to be in the block serving the welcomeBar.phtml file.

 
Magento Community Magento Community
Magento Community
Magento Community
 
IanRyan
Sr. Member
 
Total Posts:  149
Joined:  2011-01-21
 

I am reasonably new to this and do not quite follow your response. That said here is the local.xml file:-

<?xml version="1.0" encoding="UTF-8"?>
<layout>
<default>

<!-- Remove callouts and rarely used stuff -->
<remove name="top.search"/>
<remove name="right.permanent.callout"/>
<remove name="left.permanent.callout"/>
<remove name="breadcrumbs"/>
<remove name="left.newsletter" />

<reference name="footer_links">

<!-- Remove ‘Site Map’ Link - Default Position: 10
Original link adding in catalog.xml -->
<action method="removeLinkByUrl">
<url helper="catalog/map/getCategoryUrl" />
</action>

<!-- Remove ‘Search Terms’ Link - Default Position: 20
Original link adding in catalogsearch.xml-->
<action method="removeLinkByUrl">
<url helper="catalogsearch/getSearchTermUrl" />
</action>

<!-- Remove ‘Advanced Search’ - Default Position: 30
Original link adding in catalogsearch.xml-->
<action method="removeLinkByUrl">
<url helper="catalogsearch/getAdvancedSearchUrl" />
</action>

</reference>

<!-- add the local stylesheet -->
<reference name="head">
<action method="addCss">
<stylesheet>css/local.css</stylesheet>
</action>
</reference>

<!-- add the main navigation bar -->
<reference name="header">
<block type="core/template" name="main.nav.bar" as="mainNavBar" template="page/html/mainNavBar.phtml" />
</reference>

<!-- add the welcome bar -->
<reference name="header">
<block type="core/template" name="welcome.bar" as="welcomeBar" template="page/html/welcomeBar.phtml" />
</reference>

<!-- add the sub navigation bar -->
<reference name="footer">
<block type="core/template" name="sub.nav.bar" as="subNavBar" template="page/html/subNavBar.phtml" />
</reference>

<!-- add the currency selector -->
<reference name="right">
<block type="directory/currency" before="right.permanent.callout" name="currency" template="directory/currency.phtml" />
</reference>

</default>
</layout>

Again the this->getWelcome() returns the configured welcome message when the php statement is included in header.phtml but does not return the configured welcome message when the php is included in welcomeBar.phtml.

 
Magento Community Magento Community
Magento Community
Magento Community
 
brobie
Sr. Member
 
Avatar
Total Posts:  139
Joined:  2008-07-28
 

Okey Dokey.

Because you are new at this, this might fly over your head but…

When the layout is being rendered it uses the combination of Blocks and PHTML (templates files).  Every PHTML is assigned to a Block Class so when you say $block->getHtml() is will then render the phtml file that is assigned to it.

So in your case, you have the header block which is more than likely this block: page/html_header (which is Mage_Page_Block_Html_Header) and the template that is assigned to that is: page/html/header.phtml (this is hardcoded in the Mage_Page_Block_Html_Header block).

In your layout xml, you have added the block : <block type="core/template" name="welcome.bar" as="welcomeBar" template="page/html/welcomeBar.phtml" /> as a child block of the header block, and you have chosen to use the block type of “core/template” which is Mage_Core_Block_Template, and you have assigned your welcomeBar.phtml to be rendered when the block welcomeBar’s ->getHtml() is called.

When you call $this->getWelcome() from the header.phtml, it is actually calling a function called getWelcome() in the Mage_Page_Block_Html_Header class.  If that function doesn’t exist it uses the magic getters and setters.  So SOMEWHERE you are setting the welcome message into that block.

However, if you call $this->getWelcome() from the welcomeBar.phtml it is actually calling a function called getWelcome() in the Mage_Core_Block_Template class.  If that function doesn’t exist it uses the magic getters and setters.

So my guess is that the header block has the data that it needs, and the Mage_Core_Block_Template doesn’t.

 
Magento Community Magento Community
Magento Community
Magento Community
 
uguptu
Sr. Member
 
Avatar
Total Posts:  125
Joined:  2010-02-01
Kyiv, Ukraine
 

Just to sum it up: if you want to have a custom template for a Mage_Core_Block_Template instance, it’s ok. But you must understand that to fetch some data in the template, you must have prepared that data somewhere and put it into the block.

There are two ways: in case the block is of one of “standard” types, like Mage_Core_Block_Template, you need to PUSH the necessary data in the controller, by calling this:

public function someAction()
{
    
// ...
    
$this->loadLayout();
    
$myBlock $this->getLayout()->getBlock('block_name');
    if (
$myBlock{
        $myBlock
->setWelcomeMsg(Mage::helper('myhelper')->__('Blah-blah-blah'));
    
}
    
// ...
}

and then fetch the same data in the template:

<?php echo $this->getWelcomeMsg() ?>

Another way is to derive a CUSTOM block from Mage_Core_Block_Template, and implement a real getter getWelcomeMsg() in it. That getter will PULL necessary data from whatever place you want by itself.

Hope the difference between pushing and pulling data in blocks is clear. And it’s obvious that core Magento blocks can pull some specific data they are designed for, but they unaware of anything that you have pushed into them unless you set your own template that knows what has been pushed.

Hope it helped. and, guys, I beg you to use the code tag for your code, it’s a disaster otherwise. Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
uguptu
Sr. Member
 
Avatar
Total Posts:  125
Joined:  2010-02-01
Kyiv, Ukraine
 

I am not going to negate all I have written down earlier, but now I’ve realized I didn’t really understand what was your initial request about.

Look, the Mage_Page_Block_Html_Header just has it’s own getWelcome() getter, that’s it. What you are trying to do is trying to get a value that doesn’t exist right in that block (Mage_Core_Block_Template). However, there is a special block Mage_Page_Block_Html_Welcome that does it for you. Simply put a block of this type somewhere and make sure it is rendered. By rendering itself would give you your welcome message any place you want.

Hope it helps.

 
Magento Community Magento Community
Magento Community
Magento Community
 
copperlab
Jr. Member
 
Total Posts:  19
Joined:  2009-02-05
 

I am trying to apply this solution to a problem I am having with a full page cache solution.  I need the welcome message to be unset and hence in its own block.

I think I have made some progress for this problem.  However I am not getting the solution.  What I have done is created a new block in the app/design/frontend/default/layout/page.xml file. 
I have added this:

<block type="page/html_welcome" name="testwelcome" as="testwelcome"/>

There seems to be a built in core function called “welcome”.  It can be viewed at app/code/core/Mage/Page/Block/Html/Welcome.php.  So That is the reference in the page.xml file.

Then in the header.phtml file in app/design/frontend/default/template/page.html I placed a call for:

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

And finally I created a new template file called testwelcome.phtml in app/design/frontend/default/template with the following code:

<p class="welcome-msg"><?php echo $this->getWelcome() ?> <?php echo $this->getAdditionalHtml() ?></p>

I can get the welcome message to display, it does not render when I unset the block testwelcome.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top