I have a Magento store with two separate headers. Let's say out of 20 pages 15 use header.phtml and 5 use headerGreen.phtml. I've already created my custom layout module however I can't seem to get the new header to appear on select pages. The new headerGreen.phtml file has been created as well. The issue lies when I try to call the new header in the newly created layout page.
<?php echo $this->getChildHtml('headerGreen') ?>
I dont think I've added it correctly to the local.xml and was hoping someone could help.
<default>
<reference name="header">
<block type="core/template" name="header" template="page/headerGreen.phtml" />
</reference>
</default>
The function getChildHtml is expecting you to send a name of a child in the layout as first parameter
app/code/core/Mage/Core/Block/Abstract.php defines it :
/**
* Retrieve child block HTML
*
* #param string $name
* #param boolean $useCache
* #param boolean $sorted
* #return string
*/
public function getChildHtml($name = '', $useCache = true, $sorted = false)
{
// the code is does not really matter here
}
So based on your layout you should actually do
<?php echo $this->getChildHtml('header') ?>
Because header is the name attribute in this xml node of your layout :
<block type="core/template" name="header" template="page/headerGreen.phtml" />
As said in the comments, your layout is also looking strange because you are actually telling that your new header is going to be a child of the current header.
Two thinks you can do :
Use the powerfulness of the action nodes :
<default>
<reference name="header">
<action method="setTemplate">
<template>page/headerGreen.phtml</template>
</action>
</reference>
</default>
Or redefine the whole header block
<default>
<reference name="root">
<block type="core/template" name="header" as="header" template="page/headerGreen.phtml" />
</reference>
</default>
I have this block in layout file:
<block type="core/template_facade" name="product.info.container2" as="container2">
<action method="setDataByKey"><key>alias_in_layout</key><value>container2</value></action>
<action method="setDataByKeyFromRegistry"><key>options_container</key><key_in_registry>product</key_in_registry></action>
<action method="append"><block>product.info.options.wrapper</block></action>
<action method="append"><block>product.info.options.wrapper.bottom</block></action>
</block>
And this block:
$item_block = Mage::getSingleton('core/layout')->createBlock('catalog/product_list','item')->setTemplate('sns/ajaxcart/catalog/product/item.phtml')->setData('product', $product);
How do I have the upper block as $item_block's child programmatically? I know it should be something like:
$upper_block = <create upper block>;
$item_block->setChild('somealias',$upper_block);
However, the upper block seems to be too complicate for me to know how to create it programmatically. Please help!
On the block level you can do it like this:
protected function _prepareLayout()
{
$block = $this->getLayout()->createBlock('catalog/product_list')
$this->setChild('items', $block);
return parent::_prepareLayout();
}
Try this
$upper_block = Mage::getSingleton('core/layout') ->createBlock('core/template_facade', "product.info.container2");
$upper_block->setDataByKey('alias_in_layout', 'container2');
$upper_block->setDataByKeyFromRegistry('options_container', 'product');
$upper_block->append('product.info.options.wrapper');
$upper_block->append('product.info.options.wrapper.bottom');
$item_block = Mage::getSingleton('core/layout')->createBlock('catalog/product_list','item')->setTemplate('sns/ajaxcart/catalog/product/item.phtml')->setData('product', $product);
$item_block->setChild('somealias',$upper_block);
I am using magento 1.7 and i have create custom widget block but some reason i am unable to render template i don't know why please give right solution and what is the problem with my code
if i return string code from _toHtml then its showing code but through phtml template file i am unable to render anything
block\slider.php
<?php
class XXXX_Slider_Block_Slider extends Mage_Core_Block_Template
{
/**
* Constructor. Set template.
*/
protected function _construct()
{
parent::_construct();
$this->setTemplate('slider/slider.phtml');
}
protected function _toHtml()
{
return parent::_toHtml();
}
}
and this is my slider/slider.phtml
slider is executed
and here is following my cms.xml
<?xml version="1.0"?>
<!--
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license#magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* #category design
* #package base_default
* #copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
* #license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
-->
<layout version="0.1.0">
<!--
Default layout, loads most of the pages
-->
<default>
<reference name="footer">
<block type="cms/block" name="cms_footer_links" before="footer_links">
<!--
The content of this block is taken from the database by its block_id.
You can manage it in admin CMS -> Static Blocks
-->
<action method="setBlockId"><block_id>footer_links</block_id></action>
</block>
</reference>
</default>
<cms_page translate="label">
<label>CMS Pages (All)</label>
<reference name="content">
<block type="slider/slider" name="easyshop.slider" template="slider/slider.phtml"/>
<block type="core/template" name="page_content_heading" template="cms/content_heading.phtml"/>
<block type="page/html_wrapper" name="cms.wrapper" translate="label">
<label>CMS Content Wrapper</label>
<action method="setElementClass"><value>std</value></action>
<block type="cms/page" name="cms_page"/>
</block>
</reference>
</cms_page>
<cms_index_index translate="label">
<label>CMS Home Page</label>
<remove name="cms_page" />
</cms_index_index>
<cms_index_defaultindex>
<remove name="right"/>
<remove name="left"/>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<reference name="content">
<block type="core/template" name="default_home_page" template="cms/default/home.phtml"/>
</reference>
</cms_index_defaultindex>
<cms_index_noroute translate="label">
<label>CMS No-Route Page</label>
</cms_index_noroute>
<cms_index_defaultnoroute>
<remove name="right"/>
<remove name="left"/>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<reference name="content">
<block type="core/template" name="default_no_route" template="cms/default/no-route.phtml"/>
</reference>
</cms_index_defaultnoroute>
</layout>
Please if you think question need more improvement then you can edit but please don't do unnecessary down vote this is my problem i am sharing here
Why do you extend your block from Mage_Core_Block_Abstract? Abstract class has no property $_template and your _toHtml() method does not implement template output. You need to extend from Mage_Core_Block_Template class.
Magento has several block types. You can find good list in this answer.
I use the function _getUrl to link the top link I created in my custom module, but for some reason it duplicates the URL. For example, say I want it to link me to:
www.localhost.com/magento/sapna/account/index
instead, it shows:
www.localhost.com/magento/http://index/www.localhost.com/magentosapna/account/index
Here is my function:
const ROUTE_FORM = 'sapna/account/index';
/**
* Retrieve goomer/breeder registariion url
*
* #return string
*/
public function getLoginUrl(){
return $this->_getUrl(self::ROUTE_FORM, $this->getLoginUrlParams());
}
i'm doing in module/Helper/Data.php and i use getUrl it does not work
I wrote this in the Data.php helper file. Can anyone help? I have been through this so many times, but I do not see what is wrong.
Check your customer.xml file for the following :
<customer_logged_out>
<reference name="top.links">
<action method="addLink" translate="label title" module="customer">
<label>Log In</label>
<url helper="customer/getLoginUrl"/>
<title>Log In</title>
<prepare/>
<urlParams/>
<position>100</position>
</action>
</reference>
<remove name="reorder"></remove>
</customer_logged_out>
/* Use <prepare/> instead of <prepare>true</prepare> */
worked for me...!
Not sure why this is so difficult. If I understand THIS correctly, I should be able to swiftly accomplish my goals... But no joy.
So - I'm building my first theme, and still getting my head around layout...
I'm working specifically on the Catalog Product View page, and am converting this page from a right column layout to a left column layout. I simply want to move the blocks from the right into the left.
In the default catalog.xml, product_list_related is defined:
</catalog_product_view>
//...
<reference name="right">
<block type="catalog/product_list_related" name="catalog.product.related" before="-" template="catalog/product/list/related.phtml"/>
</reference>
</catalog_product_view>
In my local.xml, I'm simply trying to move this block:
<layout>
// bunch other page directives
<catalog_product_view>
<reference name="root">
<action method="setTemplate"><template>page/2columns-left.phtml</template></action>
</reference>
<reference name="right">
<action method="unsetChild"><name>catalog.product.related</name></action>
</reference>
<reference name="left">
<action method="insert"><blockName>catalog.product.related</blockName></action>
// note that that "catalog.leftnav" gets inserted as expected
<block type="catalog/layer_view" name="catalog.leftnav" after="-" template="catalog/layer/view.phtml"/>
</reference>
</catalog_product_view>
</layout>
As noted - inserting catalog.leftnav works as expected, so I'm assuming everything else is set up correctly. The target block renders as expected if I leave the template and other directives unchanged, which tells me that the block should render once its been properly unset and inserted...
This is driving me nuts... but what else is new with Magento.
Cheers -
b[]x
UPDATE
Because I simply can not get a local.xml override to work, I'm just falling back on a modified catalog.xml. I'm a reasonably smart guy... it worries me that I can't get this to work (and that magento just silently fails, whatever the case ) - but I can't waste anymore time dicking around with this stupid issue.
Moving on.
UPDATE, again.
Spent some time, now, working in magento and getting more familiar with its complexities. I came back to this issue today as I need to get my local.xml working right.
I really don't know what I had wrong, but this set of directives finally worked.
<reference name="right">
<action method="unsetChild">
<alias>catalog.product.related</alias>
</action>
</reference>
<reference name="left">
<action method="insert">
<block>catalog.product.related</block>
</action>
</reference>
A key point I'll make note of for others dealing with this:
Layout xml directives call available methods within magento classes. In this case, Page.xmls "Left" block is of type Mage_Core_Block_Text, which inherits from Mage_Core_Block_Abstract which contains the methods unsetChild and insert.
from Mage_Core_Block_Abstract :
/**
* Unset child block
*
* #param string $alias
* #return Mage_Core_Block_Abstract
*/
public function unsetChild($alias)
{
if (isset($this->_children[$alias])) {
unset($this->_children[$alias]);
}
if (!empty($this->_sortedChildren)) {
$key = array_search($alias, $this->_sortedChildren);
if ($key !== false) {
unset($this->_sortedChildren[$key]);
}
}
return $this;
}
and
/**
* Insert child block
*
* #param Mage_Core_Block_Abstract|string $block
* #param string $siblingName
* #param boolean $after
* #param string $alias
* #return object $this
*/
public function insert($block, $siblingName = '', $after = false, $alias = '')
{
if (is_string($block)) {
$block = $this->getLayout()->getBlock($block);
}
if (!$block) {
/*
* if we don't have block - don't throw exception because
* block can simply removed using layout method remove
*/
//Mage::throwException(Mage::helper('core')->__('Invalid block name to set child %s: %s', $alias, $block));
return $this;
}
if ($block->getIsAnonymous()) {
$this->setChild('', $block);
$name = $block->getNameInLayout();
} elseif ('' != $alias) {
$this->setChild($alias, $block);
$name = $block->getNameInLayout();
} else {
$name = $block->getNameInLayout();
$this->setChild($name, $block);
}
if ($siblingName === '') {
if ($after) {
array_push($this->_sortedChildren, $name);
} else {
array_unshift($this->_sortedChildren, $name);
}
} else {
$key = array_search($siblingName, $this->_sortedChildren);
if (false !== $key) {
if ($after) {
$key++;
}
array_splice($this->_sortedChildren, $key, 0, $name);
} else {
if ($after) {
array_push($this->_sortedChildren, $name);
} else {
array_unshift($this->_sortedChildren, $name);
}
}
$this->_sortInstructions[$name] = array($siblingName, (bool)$after, false !== $key);
}
return $this;
}
Local xml parameters are important, then, not in name (specifically), but in order ie:
<reference name="left">
<action method="insert">
<block>catalog.product.related</block>
<siblingName>catalog.leftnav</siblingName>
<after>1</after>
<alias>catalog_product_related</alias>
</action>
</reference>
Ultimately, this makes local.xml a really powerful method of manipulating the system, but if you are unfamiliar with it and the magento system, get ready for weeks or months of work to really get your head around it.
Cheers
Yet another update
I've run into the problem several times now, where a block I want to move has been removed. This is a problem, as any block that has been removed from layout is nuked forever.
However, with Alan Storm's very handy Unremove Plugin you can undo whats been done:
<checkout_onepage_index>
<x-unremove name="left" />
<reference name="right">
<action method="unsetChild">
<alias>checkout.progress.wrapper</alias>
</action>
</reference>
<reference name="left">
<action method="insert">
<block>checkout.progress.wrapper</block>
</action>
</reference>
</checkout_onepage_index>
It manages this feat by watching the layout object, and building a list of removed blocks, which can be later referenced.
Nice!
This is a dusty thread now, but for the record, this is the final answer I went with.
<reference name="right">
<action method="unsetChild">
<alias>checkout.progress.wrapper</alias>
</action>
</reference>
<reference name="left">
<action method="insert">
<block>checkout.progress.wrapper</block>
</action>
</reference>
Magento recommends including this in your local.xml, and it has proved an effective technique.
An alternative approach is to rename the "right" block to "left" rather than move blocks from right to left. However, this will not work if the "right" block is being removed in the first place as I suggested might be the case in my other answer.
<remove name="left" />
<reference name="right">
<action method="setNameInLayout"><name>left</name></action>
</reference>
<reference name="root">
<action method="setChild"><alias>left</alias><name>left</name></action>
<action method="unsetChild"><alias>right</alias></action>
</reference>
<!-- make sure nothing referencing "right" comes after this! -->
If you are just looking to change the output from 2col-right, to 2col-left, it would have been much, much easier to just change
<reference name="right" ...
to
<reference name="left" ...
There is no need to unset or insert any children or re-declare anything. You are overcomplicating things and duplicating code needlessly.
If you are making your own very besoke design too, I would suggest starting copying the entire ./app/design/frontend/base directory to ./app/design/frontend/mypackage - then work from the mypackage/default directory to re-skin your site. Its cleaner and easier. Others may comment on the potential issues of upgrade-ability if you copy every file (rather than just the ones you intend to modify), but it is by far a better practice and much less prone to errors, difficulties and general maintenance.
Edit: To follow up on this in more detail - have a look https://magento.stackexchange.com/a/3794/361
The "right" block is probably being removed with a <remove name="right" /> at some point in the layout. If so, this will cause the "catalog.product.related" block to never be added to the layout in the first place so there is no block to insert into the "left" block.
I'm pretty sure this is the issue, but to confirm, add a Mage::log("Removed $blockName"); in Mage_Core_Model_Layout->generateXml() inside the for loop and check the log after loading the page.
If I'm right, simply copy the <block..> into your local.xml and remove the "unsetChild" and "insert" actions.
I would do it this way: In your local.xml under e.g.
<reference name="right"></reference>
you remove the blocks, e.g.:
<remove name="right.poll">
and then you add the blocks in
<reference name="left"></reference>
Copying base/default to a local theme is a horrible idea. Every update to a core theme file on a Magento version upgrade leads to the same vulnerability of modifying app/code/core files -- needing you to diff the files out on upgrades.
Your proper course of action is modifying a single local.xml with your additions or overrides. If that doesn't suffice, use the proper syntax to create a module in app/code/community with your layout definitions and custom layout XML files.
I think the best answer to modify Magento layouts is given on Classyllama.com
http://www.classyllama.com/development/magento-development/the-better-way-to-modify-magento-layout
When you use the remove tag, it removes any blocks with the specified name from the entire layout, regardless of the context. So, if I remove right.newsletter in the context and that name is used in say the context, then both blocks will be removed. Because remove operates on the global context, you can only remove an element once. Since is being called in catalogsearch.xml, we have to unset it, or else we'll get an error.
<action method="unsetChild"><name>right.newsletter</name></action>;