Magento - How to create a getChildHtml block and call it in another - magento

Currently i'm working to redesign a custom Magento template.
I'm facing the following problem.
In file called: 2columns-left.phtml i am facing code like this several times:
<?php echo $this->getChildHtml('global_messages') ?>
or
<?php echo $this->getChildHtml('global_messages') ?>
These code lines makes me think that they are calling blocks and the content in them by getting the name of .phtml file.
So i have file called slider_layred_nav.phtml and i want to call all the content in it in file 2columns-left.phtml so i tried this code:
So in file 2columns-left.phtml i put:
<?php echo $this->getChildHtml('slider_layred_nav') ?>
But it is just not displaying anything.
I assume that i have to do something with the layout but i have no idea what.
Can you help me our resolve this mystery ?
Thanks in advance!

I guess you have some problem understanding the magento layouts and how the blocks are rendered in the template files.
In simple words,getChildHtml() renders all the blocks inside that particular block i.e child blocks of that parent block.If you use getChildHtml('slider_layred_nav') than it will render the block named slider_layred_nav not the template file.
Here $this has it own meaning. It refers to the block which has template file 2columns-left.phtml.
At first you have to create a block inside the block which use the template 2columns-left.phtml
For example:
<block type="core/template" name="slider_layred_nav" template="something/slider_layred_nav.phtml"/>
After creating this block inside the block that uses the template 2columns-left.phtml you can do echo $this->getChildHtml('slider_layred_nav'); to display that block inside your 2columns-left.phtml file.
Hope i made you clear to some extent.

You are indeed missing the layout part.
If you open up page.xml (under layout folder of your theme), you can find the definition of the block global_messages like so :
<block type="core/messages" name="global_messages" as="global_messages"/>
But don't take that block as example, since it is a special one which does not have a template associated with it.
The cleaner way to make what you want is like so :
Create a local.xml file under your in app/design/frontend/your_package/your_theme/layout (nota: your_package & your_theme will vary).
Then paste this one in the new local.xml file :
<?xml version="1.0"?>
<layout version="0.1.0"><!-- everything goes in here -->
<default>
<block type="core/template" name="slider_layred_nav" template="path/to/slider_layred_nav.phtml"/>
</default>
</layout>
path/to/slider_layred_nav.phtml being the path to your phtml from app/design/frontend/your_package/your_theme/template/
slider_layred_nav in the name attribute of the block being the name passed as parameter when you are calling $this->getChildHtml('some_name').

Related

Magento static block is not showing on my layout although I followed the instructions correctly

I want to add a static block on my layout. Therefore from the backend I add a new static block and gave an identifier for that. Then I put my block code inside the page.xml file. This is my code,
<block type="cms/block" name="templatename" template="page/html/templatename.phtml">
<action method="setBlockId"><block_id>my_id</block_id></action>
</block>
Then I put the php code inside my phtml (templatename.phtml) file to show the data. This is my code,
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_id')->toHtml();?>
Finally my phtml template file is loading in my layout.phtml file. This is that code,
<?php echo $this->getChildHtml('templatename') ?>
But my static block isn’t showing. Why is that? My Magento version is 1.8
if you want to call cms static block in your custom phtml file
just use below code to call with unique id define by you in admin cms static block section
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_id'')->toHtml()?>
that will sure display your static block in to you custom phtml files.
or you can use detail link
http://importantmagento.blogspot.in/2012/06/magento-how-to-call-static-block-on.html
i am not associate with above link for any how. it is just for knowledge purpose.
hope this will sure help you.
Thanks everyone....finally i could solve the issue.Only thing i was done,remove the block declaration from the xml file.I just write the php code on my template.Now it works fine.

magento $this->getCurrencies() can't work?

i put the code in my theme header.phtml
echo $this->getCurrencyCount() and $this->getCurrentCurrencyCode() both of them are no any output. but i have set 4 currencies for the site.
but in the same file, $this->getWelcome() have the right output.why?
when i echo Mage::app()->getStore()->getCurrentCurrencyCode(); in the file ,it has a value. thank you.
if i want to output the currency switcher. how do i do?
These function are from the Mage_Directory_Block_Currency model, not the Mage_Page_Block_Html_Header one.
You might look at the directory/currency.phtml file to handle this kind of task
IF you want to determine the best position in the page thanks to the header.phtml file, just define this block as a child of the header one.
In your theme layouts, in directory.xml, in the <default> area add this :
<reference name="header">
<block type="directory/currency" name="header_currency" before="catalog.leftnav" template="directory/currency.phtml"/>
</reference>
Then in the header.phtml file just add echo $this->getChildHtml('currency'); where you need it to show.
If you just need these variables without showing the currency block use in your header.phtml this code
$currency_block = new Mage_Directory_Block_Currency;
$currency_block->getCurrentCurrencyCode();
The last part is provided as is and is untested.

Magento blocks - still not getting it

I have a custom template block (phtml) that is defined like this:
<block type="catalog/product" name="a.unique.name" template="dir/dir/customlist.phtml"/>
that is defined in CMS home page within this block
<reference name="left">
However if I move it to header block, it does not appear
<reference name="header">
What am I missing here?
** ---- addition -----
I tried this as suggested by D. Sloof, but it does not work. (I suspect more due to my mis-actions than his explanation.)
I added getChildHtml('customlist'); ?> to
mytheme\template\page\html\header.phtml
just under div "top-col-1"
<div class="wrapper">
<div class="top-col-1">
<?php echo $this->getChildHtml('customlist'); ?>
where "customlist" can befound in
mytheme/template/dir/dir/customlist.phtml
What am I doing wrong?
got it...
block name a.unique.name in xml file
<block type="catalog/product" name="a.unique.name" template="dir/dir/customlist.phtml"/>
must match what is called in mytheme\template\page\html\header.phtml
<?php echo $this->getChildHtml('a.unique.name'); ?>
The header block is of type Mage_Page_Block_Html_Header and will not automatically render child blocks. You can have a look at the template file page/html/header.phtml how the child block html is retrieved.
You essentially have 2 options:
Add your own getChildHtml call in the header template (by copying it to your own theme), or;
Put your block inside a container that will automatically render child blocks. The left block is of type Mage_Core_Block_Text_List which will do this. There is a similar block inside the header block called top.container that is of type Mage_Page_Block_Html_Header that will do this as well: you can reference this block instead.

Magento Including a CUSTOM phtml file in view.phtml

I am trying to work out how to create custom phtml files to include on view.phtml (and ultimately to be called from any default Magento phtml file).
I have created a seperate phtml file with the content I want in it called productbadges.phtml
This will be pulled through as the last item in
I understand the callout usually is
<?php echo $this->getChildHtml('phtmlfilename') ?>
However I know I need to do add something to catalog.xml so Magento recognizes the callout and can source the correct file. But I do not properly understand Magento's XML syntax.
Could anyone assist?
vicch's response is the correct way of doing it.
However, it's also helpful to know that there is an alternate method:
$block = $this->getLayout()->createBlock(
'Mage_Core_Block_Template',
'choose_a_block_name',
array('template' => 'folder/myphtmlfile.phtml')
);
I am posting this for general knowledge. This is not the accepted way of doing this, since it is not consistent with how Magento templates and blocks are used.
you can use
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('goodtest/test.phtml')->toHtml(); ?>
see also here:
How do i call .phtml block at specfic page in magento?
and
want to call one phtml file in another phtml file using anchor tag
Given the information you provided, I can only give a general solution.
First you need to find the layout XML for this view.phtml. You should be looking for something like:
<block type="..." name="..." ... template="../view.phtml">
To add the declaration of the new template directly under the wrapping block, it should be:
<block type="..." name="..." ... template="../view.phtml">
<block type="..." name="phtmlfilename" template="../phtmlfilename.phtml"/>
...
</block>
It is also possible to reference the outter block somewhere else:
<reference name="[name_of_view.phtml_block]">
<block type="..." name="phtmlfilename" template="../phtmlfilename.phtml"/>
</reference>
Type of the new template is the class name, which should be core/template or a subtype of it.
The answer to this question is below codes,just change "directory/acc_drop.phtml" to your file path name.
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('directory/acc_drop.phtml')->toHtml(); ?>

In Magento, can I add a static block to the header by xml only?

I am trying to customize a theme using only local.xml whenever possible. I want to add a static block to the header without modifying header.phtml. This code works fine for the content area, but not for the header:
<default>
<reference name="content">
<block type="cms/block" name="how-it-works-button">
<action method="setBlockId"><block_id>how-it-works</block_id></action>
</block>
</reference>
</default>
Anybody know why? I thought that all I would need is to change “content” to “header”, but nothing shows up when I do.
Thanks for your help!
The content block is a special block known as a core/text_list block (PHP class Mage_Core_Block_Text_List). These blocks will automatically render out any child blocks that are added to them.
The header block, on the other hand, is a page/html_header block (PHP class Mage_Page_Block_Html_Header). This block class inherits from Mage_Core_Block_Template, making it a core/template block. Template blocks will only render sub-blocks if their corresponding phtml template requests the block. So, by adding your block to the header, you're only doing half the work you need to. You'll need to create a custom phtml template.
The simplest way to do this (post 1.4.1.1 is to, in your own theme, create a file at
template/page/html/header.phtml
And then at the end of this file add
<?php echo $this->getChildHtml('how-it-works-button'); ?>
Assuming you've added a block to header block via layout xml, this should render your template.
Please Try this
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('how-it-works')->toHtml() ?>
And this code in header.phtml
add output="toHtml" in the block tag. I think it is only that.

Resources