Magento blocks - still not getting it - magento

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.

Related

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

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').

Magento 1.9: Add custom block to header

I have this in my local.xml.
<reference name="header">
<block type="core/template" name="wc-customheadblock" template="wc-customheadblock.phtml" />
</reference>
Inside wc-customheadblock.phtml i have:
<div style="height:100px; background-color:green">
<h1>This is the wc custom head block</h1>
</div>
But this block is not appearing in the header. I've tried the same for the sidebars (by changing the xml reference name) and it works just fine. Why does this not work for the header?
This site claims its possible to make add blocks to the header using local.xml!
http://www.classyllama.com/development/magento-development/the-better-way-to-modify-magento-layout
Go to your header.phtml file (app/design/frontend/package/theme/page/html/header.phtml) and place one of the following lines:
<?php echo $this->getChildHtml(); ?>
or
<?php echo $this->getChildHtml('wc-customheadblock'); ?>
Most likely header doesn't render child blocks by default because it would cause some template crashes. On the sidebars, extra childs can be easily appended at the end.

Cannot display block from template - magento

I'm calling a block from a template as follows. I'm aware it's not the recommended way but the layout.xml approach is giving me trouble
echo $this->getLayout()->createBlock('shoppingbasket/options')
->setTemplate('shoppingbasket/cart/item/default/options.phtml')
->setBlockId('options')
->setProduct($_item->getProduct())
->setSelectedOptions($this->getOptionList())
->toHtml() ?>
The block gets rendered. Now I'm trying to add it via the layout.xml approach
I've added the block to the layout.xml as follows:
<layout>
<checkout_cart_index>
<reference name="checkout.cart">
<block type="shoppingbasket/options" template="shoppingbasket/cart/item/default/options.phtml" name="options"></block>
<action method="addItemRender"><type>simple</type><block>checkout/cart_item_renderer</block><template>shoppingbasket/cart/item/default.phtml</template></action>
</reference>
</checkout_cart_index>
</layout>
and I'm calling the block from the template as follows:
<?php echo $this->getChildHtml('options') ?>
The block isn't appearing and I've tried moving the block everywhere in the layout.xml. What am I doing wrong? Also is there a way to pass parameters to the block using the layout.xml approach as I did with
->setProduct($_item->getProduct())
->setSelectedOptions($this->getOptionList())
Thanks!
you did everything right, except one thing: you should set block alias - "as" attribute of the block.
<block type="shoppingbasket/options" template="shoppingbasket/cart/item/default/options.phtml" name="options" as="options"></block>
Methods getChild or getChildHtml use block alias for retrieving block instance from layout.

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