Magento creating CMS blocks from xml block - magento

<reference name="left">
<!-- Layered Navigation Block -->
<block type="catalog/layer_view" name="catalog.leftnav" template="catalog/layer/view.phtml" >
<action method="setCategoryId"><category_id>2</category_id></action>
</block>
</reference>
I was able to create a CMS block from the above code
{{block type="catalog/layer_view" category_id="2" name="catalog.leftnav" template="catalog/layer/view.phtml"}}
How would I create a CMS block from the XML below...it have a bit more to it.
<reference name="content">
<block type="catalog/product_list" name="home" template="catalog/product/list.phtml">
<!-- Product List View -->
<action method="setCategoryId"><category_id>2</category_id></action>
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
</reference>

Unfortunately you cannot do what you need.
Here is why. When using the {{block}} directive magento automatically calls toHtml after creating the block and returns you the html (rendered block).
This directive also does not support blocks inside blocks.
When working with layout files, the blocks are not rendered automatically. First the layout file is parsed, the block classes are instantiated, they are assigned as child blocks or parent blocks and everything is rendered at the end.
In your case you will need something like this:
{{block type="catalog/product_list" template="catalog/product/list.phtml" name="home" toolbar_block_name="product_list_toolbar"}}
But this won't work because the block with the name product_list_toolbar does not exist yet.
If you try to create it before with something like this:
{{block type="catalog/product_list_toolbar"....}}
still won't work, because the block will just be outputed (most probably you will get an error too).
Blocks generated using {{block}} are not added to the layout so they cannot be referenced in other blocks.

Related

How to place my custom block inside another block in Magento using layout xml?

first of all I want to say that I have searched for this whole day on te internet and could not find what I wanted. I am a newbie here as well, so please forgive me if I broke any rule.
I am trying to develop a module which will add videos to product page along with images. I am stuck in this concept:
How do I insert my block into an existing base block ? For example, in the product page, there is a block product.info. Inside this block there is "Availability", "Price" etc.
How do I insert my custom block just below "Availability" and above "Prices" using my module's layout xml and template.
So I am trying to achieve something like this using my module's layout file:
<catalog_product_view translate="label">
<reference name="content">
<reference name="product.info">
WRITE BLOCK HERE SO THAT MY BLOCK SHOWS BELOW AVAILABLITY
</reference>
</reference>
</catalog_product_view>
Is this possible ? or Do I have to override the core class Mage_Catalog_Block_Product_View to do this ?
PS: Basically my aim is to list my videos next to images. Right now, I am able to list my videos from module, but images don't come in that case. I used
<block type="myblock/myblock" name="somename" as="media" template="abc.phtml"/>
So I want to append my block to the existing content.
I solved it. I had to rewrite the Mage_Catalog_Block_Product_View_Media .
In my class I over-rid the function _toHtml function like this:
public function _toHtml()
{
$html = parent::_toHtml();
$html.=$this->getChildHtml('media_video');
return $html;
}
where "media_video" is my block. My layout xml file:
<catalog_product_view translate="label">
<reference name="content">
<reference name="product.info">
<reference name="product.info.media">
<block type="myblock/myblock" name="somename" as="media_video" template="beta/abc.phtml"
before="-"/>
</reference>
</reference>
</reference>
</catalog_product_view>
You can add new block instead of over-right existing.
<catalog_product_view translate="label">
<reference name="content">
<reference name="product.info">
<block type="myblock/myblock" name="somename" as="media_new" template="abc.phtml"/>
</reference>
</reference>
</catalog_product_view>
Get New Block using following code in phtml file
<?php echo $this->getChildHtml('media_new') ?>
Thanks
Note output="toHtml" in the below code. This will print your block in product.info section.
<catalog_product_view translate="label">
<reference name="content">
<reference name="product.info">
<block type="myblock/myblock" name="somename" as="media"
template="abc.phtml" output="toHtml" before="-" />
</reference>
</reference>
</catalog_product_view>

Add block using Custom Layout Updates to category

I am trying use the Custom Layout Update field of a category to add something to the left sidebar for just this category but can't quite get it to work. I had it working before so I know I can do it but then my client accidentally deleted the category.
This works, so I know I have the ability to add something - but I don't want a static block:
<reference name="left">
<block type="cms/block" name="Designer Sidebar" before="-">
<action method="setBlockId"><block_id>designer_sidebar</block_id></action>
</block>
</reference>
I want to include a phtml file from my theme folder and trying this, but can't get it to output anything:
<reference name="left">
<block type="cms/block" name="Designer Sidebar" before="-" template="catalog/category/view_sidebar.phtml" />
</reference>
<reference name="left">
<block type="core/template" name="Designer Sidebar" template="catalog/category/view_sidebar.phtml"/>
</reference>

Updating custom block in CMS page

I'm having a bit of trouble understanding how to update a custom block that I include in a CMS page.
I am using the standard shortcode in the CMS page, which works fine:
{{block type="catalog/product_list" category_id="16"
template="catalog/product/slider_list.phtml"}}
I am trying to set the column count of this custom custom block, for which I've found that I need something like the following piece of code:
<block type="catalog/product_list" name="catalog.product.slider_list"
template="catalog/product/slider_list.phtml">
<action method="setColumnCount"><columns>4</columns></action>
</block>
I'm not exactly sure under which <reference /> block I should place the code... I looked in catalog.xml, and if I try to place it between <reference name="root" />, I do not get the correct behavior.
Thank you.
Edit: use of incorrect terminology; I was calling the block static, where in fact it is a custom block.
if you want to call it on home page where the reference name should be like in your local.xml or any of the xml of your extension. just add it like below
<cms_index_index>
<reference name="content">
// your custom block
<block type="catalog/product_list" name="catalog.product.slider_list"
template="catalog/product/slider_list.phtml">
<action method="setColumnCount"><columns>4</columns></action>
</block>
</reference>
</cms_index_index>
hope this will sure help you.

Insert new block to product.info doesn't work

I have created two new blocks, which should be output before and after this block:
<block type="catalog/product_view" name="product.info.addtocart" as="addtocart" template="catalog/product/view/addtocart.phtml"/>
To do this I've created a new module, which updates the layout. In the refered XML file stands this:
<catalog_product_view>
<reference name="product.info">
<block type="disablecartonlogout/output" name="disablecartonlogout.outputbefore" as="whatever" before="product.info.addtocart" template="disablecartonlogout/product-options-bottom-before.phtml" />
<block type="disablecartonlogout/output" name="disablecartonlogout.outputafter" as="whatever" after="product.info.addtocart" template="disablecartonlogout/product-options-bottom-after.phtml" />
</reference>
</catalog_product_view>
I get no output.
If I change the reference to "content", the blocks will be outputted, but on the wrong position.
Can you tell me, what I am doing wrong?
You need to explicitly call your blocks in the product.info block template (that should be catalog/product/view.phtml), at the place you want.
The method you want to use only works for some specific blocks (usually, core/text_list or core/template doing an empty getChildHtml() call, what's not the case of product.info)
<catalog_product_view>
<reference name="product.info.addtocart">
<block type="disablecartonlogout/output" name="disablecartonlogout.outputbefore" as="whatever1" before="-" template="disablecartonlogout/product-options-bottom-before.phtml" />
<block type="disablecartonlogout/output" name="disablecartonlogout.outputafter" as="whatever2" after="-" template="disablecartonlogout/product-options-bottom-after.phtml" />
</reference>
</catalog_product_view>
then go to:
app\design\frontend\default\themeName\template\catalog\product\view\addtocart.phtml
and add
$this->getChildHtml('whatever1', true, true);
$this->getChildHtml('whatever2', true, true);
Try this as it may help you. I tried something like this

Magento product display grid columns on home page

Trying to get the home page to display a 4 column grid for the display of items using a two_column_right template, in the local.xml file. Unfortunately it's taking on the three column grid I've specified for catalog pages elsewhere :/
Possibly need to insert <update handle="four_column_grid" /> under a tag referencing the home page??
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<four_column_grid>
<reference name="product_list">
<action method="setColumnCount">
<count>4</count>
</action>
</reference>
</four_column_grid>
<three_column_grid>
<reference name="product_list">
<action method="setColumnCount">
<count>3</count>
</action>
</reference>
</three_column_grid>
<default>
<!-- Header -->
<reference name="header">
<action method="unsetChild"><name>welcome</name></action>
</reference>
<!-- Root -->
<reference name="root">
<action method="unsetChild"><name>breadcrumbs</name></action>
</reference>
<reference name="footer">
<!-- Remove all the other Magento links - "Site Map, Search Terms, Advanced Search, and Contact Us" -->
<!-- <action method="unsetChild"><name>footer_links</name></action> -->
</reference>
<!-- Right sidebar -->
<reference name="right">
<remove name="paypal.partner.right.logo"/>
</reference>
</default>
<catalog_category_default>
<update handle="three_column_grid" />
</catalog_category_default>
<catalog_category_layered>
<update handle="three_column_grid" />
</catalog_category_layered>
</layout>
Short answer: you can't set values on blocks "inside" of CMS blocks using layout XML.
When loadLayout() is called in action controllers, the layout XML is processed, all blocks are instantiated, and the <action> nodes are executed. But the blocks are not yet rendered.
When renderLayout() is called the blocks are rendered by calling their toHtml() method.
If the block happens to be a cms/block (or cms/page) instance containing a {{block ...}} instance, that block will be instantiated at this time.
At this moment during the request flow all layout XML <action> nodes already have been processed.
In essence you are referencing a block instance in the layout XML that doesn't yet exist.
As a workaround, it might work for you to add the product list block to the homepage using layout XML, too. The downside is that you can't place it freely in other content of the CMS block.
<cms_index_index><!-- layout handle for the default homepage action -->
<reference name="content">
<block type="catalog/product_list" name="product_list">
<action method="setTemplate">
<template>catalog/product/list.phtml</template>
</action>
<action method="setCategoryId">
<catId>51</catId>
</action>
<action method="setColumnCount">
<count>4</count>
</action>
</block>
</reference>
</cms_index_index>
Of course you are not limited to the product list block. If you need to place the list inside of other content, you could add cms blocks to the homepage using layout XML ad well.
Note that this seems to have changed in magento ce 1.9+ when extending the rwd theme. You'll have to define to more blocks for 'name.after' and 'after'.
<cms_index_index>
<reference name="content">
<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
<block type="core/text_list" name="product_list.name.after" as="name.after" />
<block type="core/text_list" name="product_list.after" as="after" />
<action method="setCategoryId"><catId>3</catId></action>
<action method="setColumnCount"><count>4</count></action>
</block>
</reference>
</cms_index_index>

Resources