Magento template not loaded properly - magento

I created a new module and I try to load a template in an action under indexcontroller. That template loaded properly but the basic/root templates are not loaded. Magento loaded only the template file what I specified in the XML. I explained the steps below what I followed.
Create a module named 'Sample'.
Create a IndexController with a index action.
Create local.xml file under the dir app/design/frontend/default/default/layout/
Create sample_page.phtml under the dir app/design/frontend/default/default/template/sample/sample_page.phtml.
IndexController:
public function indexAction() {
$this->loadLayout();
$this->renderLayout();
}
local.xml:
<layout version="0.1.0">
<default>
</default>
<sample_index_index>
<reference name="root">
<block type="page/html" name="root" output="toHtml" template="sample/sample_page.phtml">
</block>
</reference>
</sample_index_index>
Could anybody find the issue what I done?

The issue here is that you referenced the wrong layout block ("root" in your example). Doing that replace the entire set of blocks for page by the one you specify.
To get all the blocks displayed (included yours), simply reference another layout block than "root", for example "content".
You could also want to modify the root layout block for pages of your module, if that is the case, in the default handle, reference the root block and set a different template.
Here is an example :
<layout version="0.1.0">
<default>
<reference name="root">
<action method="setTemplate">
<template>page/1column.phtml</template>
</action>
</reference>
</default>
<sample_index_index>
<reference name="content">
<block type="page/html" name="the_name_you_like" output="toHtml" template="sample/sample_page.phtml">
</block>
</reference>
</sample_index_index>
</layout>

Related

Get a block type of a page Magento

I would like to know how to find the block type of some page example : the home page.
I have a problem in my homepage, I want to display some block, in the xml I declared my block in <cms_index_index> (local.xml) but if I put it in the <reference name="head"> it appears, if <reference name="content"> or <reference name="content"> it does not work . It seems to me that the type of block is not good.
xml:
<cms_index_index>
<reference name="root">
<block type="core/template" name="seconnecter_test" template="customer/form/test-seconnecter.phtml"/>
</reference>
</cms_index_index>
in the homepage 1column-full.phtml i called it like this:
<?php echo $this->getChildHtml('seconnecter_test');?>
You can use
<reference name="root">
<block type="core/template" name="seconnecter_test" output="toHtml" template="customer/form/test-seconnecter.phtml"/>
</reference>
When you give <reference name="content"> Their block type is core/text_list. this block type output their content automatically, you don't need to use getchildhtml().
In your local.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<cms_index_index>
<reference name="content">
<block type="core/template" name="seconnecter_test" before="_" template="customer/form/test-seconnecter.phtml"/>
</reference>
</cms_index_index>
</layout>
Place the template file at correct location, and the block will render at top of content.
Or try this go to cms->pages->homepage from admin panel. Add following in the layout update xml of design section
<reference name="content">
<block type="core/template" name="seconnecter_test" before="_" template="customer/form/test-seconnecter.phtml"/>
</reference>
refresh the cache.

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>

What is preventing my layout updates from being displayed

I am trying to display a simple message on my Magento site using a special block that I have created. I have been able to easily unset blocks and insert them in other places on the home page, but I am running into trouble when I try to do the same thing on one of the product pages. I have created a file at app/design/frontend/base/default/layout/packagename/modulename.xml
with the following contents:
<?xml version="1.0"?>
<layout>
<default>
<reference name="product.info">
<block type="core/text" name="free_shipping">
<action method="setText"><text><![CDATA[<div>Free Shipping!</div>]]> </text></action>
</block>
</reference>
<reference name="header">
<action method="unsetChild">
<name>top.search</name>
</action>
</reference>
</default>
</layout>
It seems to me like the code above would remove the search bar from a product page and add a block in the product info section that says "Free shipping!" but when I load the page there are no changes. I have tried using "remove" to alter some of the blocks on the page and it works, so the file is definitely being loaded into the layout.xml. I have also tried making my changes in the local.xml file instead, with the same results. Other than that, I am kind of at a loss for things to try to get this to work correctly.
Edit: To provide some more information on the problem, if I were to replace my changes with something like
<reference name="root">
<action method="unsetChild">
<name>header</name>
</action>
</reference>
The header is sucessfully removed. So I guess the question now is, why does calling unset child work as expected when used on the "root" block but not on "header"?
I think you need to be more specific with your layout handler, you are setting it as and that means all pages, i recommend you change that handler by
<?xml version="1.0"?>
<layout>
<catalog_product_view>
<reference name="product.info">
<block type="core/text" name="free_shipping">
<action method="setText"><text><![CDATA[<div>Free Shipping!</div>]]> </text></action>
</block>
</reference>
<reference name="product.info">
<block name="header">
<action method="unsetChild">
<name>top.search</name>
</action>
</block>
</reference>
</catalog_product_view>
</layout>
Greetings.
First of all, never put your stuff in the base/default folder.
On to your question. What if you try it like this:
<reference name="header">
<action method="unsetChild">
<name>top.search</name>
</action>
</reference>
To answer your other question:
The header is sucessfully removed. So I guess the question now is, why does calling unset child work as expected when used on the "root" block but not on "header"?
It's not the fact it's on the root block, it's that you should use unsetChild within the <reference/> part.

Magento: Update block position with before/after attribute from local.xml layout reference

I need to append the before attribute to a block via a layout update reference call.
This is my local.xml file:
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
<reference name="content">
<block type="page/html_wrapper" name="content.footer" as="contentFooter" translate="label" after="-">
<label>Content Footer</label>
<action method="setElementId"><value>content-footer</value></action>
<action method="setElementClass"><value>clear</value></action>
</block>
</reference>
</default>
<catalog_category_default>
<reference name="breadcrumbs.container">
<action method="unsetChild"><name>category.title</name></action>
</reference>
<reference name="content">
<block type="catalog/category_view" name="category.title" template="catalog/category/title.phtml" before="content.footer"/>
</reference>
</catalog_category_default>
</layout>
My problem is, on the content block I create a content.footer block that you can assign widgets to in admin. I use after="-" on the content.footer block so in my mind, should put it ALWAYS at the bottom of the content block but this is not the case.
When you view a catalog category and it inserts the category.products block in to the content block, it displays underneath the content.footer block. The only way to make it work is if I redefine it in my local.xml and include all the child blocks in category.products, and set before before="content.footer".
So I thought why can't I use a reference to category.products in the catalog_category_default layout and set the block's before attribute, I tried the following:
<reference name="category.products">
<action method="setBefore"><value>content.footer</value></action>
</reference>
Which had no affect.
I also noticed the setAttribute() function in Mage_Core_Block_Abstract which saw it's just a wrapper for setData() but thought I would try it anyway, still nothing:
<reference name="category.products">
<action method="setAttribute"><key>before</key><value>content.footer</value></action>
</reference>
Is it possible to do what I want? Does before/after only apply to blocks in the same reference?
Layout updates are processed in order of layout update handles. Your block is being added last to content but only for the default LUH. Other handles (catalog_product_view, catalog_category_layered, etc) are processed after this handle.
If you truly need a content footer everywhere and want to make sure that it is the last thing inside of the content div, you should add your block to the root node in the default handle and customize the root templates (directly under page/, e.g. page/1column.phtml) by adding a getChildHtml('your.block') call after the getChildHtml('content') call. This will ensure that your block is always immediately at the end of the content blocks.

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