I am using magento 1.9.2.2 version. I want to add content from custom module before footer (after all page content) without change any core file and theme files ( .phtml ) .
-
Go through the article http://inchoo.net/magento/custom-reference-structural-block/ <br/> It has all the stuff that you require. – Anshu Mishra Aug 24 '16 at 11:13
-
Please read comment in that post. its not working in 1.9 magento version. I tested it. Its Not working :( – Chirag Rajput Aug 24 '16 at 12:33
This is possible with CMS configuration only and no code:
create a static block (CMS > Static Blocks) that contains your content
Create a new widget instance (CMS > Widgets) and select your theme
Add Layout Update for page "All Pages" and reference "Page Footer" (this is a container within the page footer) - or "Content" if you want it to be part of the content (i.e. not span over all columns)
4. Select static block from (1) in "Widget Options":
Save.
Clean cache.
Without manual configuration
If this is for an extension, you can use a custom template and the following layout XML instead:
<default>
<reference name="bottom.container">
<block type="core/template" before="-" name="example" template="your_module/your_template.phtml" />
</reference>
</default>
The template will be part of the footer, if you want it to be part of the content, see below:
Add a block programmatically to all pages at the end of the content
Unfortunately, this does not work as desired:
<default>
<reference name="content">
<block type="core/template" after="-" name="example" template="your_module/your_template.phtml" />
</reference>
</default>
This is because the default
layout handle is applied before any page specific layout handles, so although we add the custom block "after all blocks that are already added", the actual content will be added later, after our custom block.
So you have to add your block after all layout XML updates are processed and this is possible with an observer for the controller_action_layout_load_before
event. The observer will look like this:
public function addBlockAfterContent(Varien_Event_Observer $observer)
{
$layout = Mage::getSingleton('core/layout');
$content = $layout->getBlock('content');
$newBlock = $layout->createBlock('core/template', 'example_block_name');
$newBlock->setTemplate('your_module/your_template.phtml');
$content->append($newBlock);
}
-
That would add the block before all other layout updates are loaded and would fail because "content" does not exist yet. – Fabian Schmengler Aug 26 '16 at 8:20
-
Great, then I was wrong :) I looked at the code and the event is triggered after the XML handles are added, so it makes sense that it works and it's actually a more fitting event than my suggestion – Fabian Schmengler Aug 26 '16 at 8:41
-
@ChiragRajput now that everything is clarified in the answer, we can delete the comments – Fabian Schmengler Aug 26 '16 at 19:33
-
-
@FabianSchmengler, where do I have to put the observer code
addBlockAfterContent
. In which file? – Black Jun 1 '18 at 11:36
It is only possible with this type of core/text_list block
.
core/text_list
: Some blocks like content, left, right etc. are of type core/text_list
. When these blocks are rendered, all their child blocks are rendered automatically without the need to call the getChildHtml()
method.
Otherwise you have to use getChildHtml()
in your theme file.
You can use like:
<reference name="content">
<block type="core/template" after="-" name="test2" template="test.phtml" />
</reference>
-
-
If you are using core/text_list block then no need to change any theme file or core file. For this you can use reference name "before_body_end" and you can use before="footer-block-name". – Ashish Jagnani Aug 24 '16 at 12:01
-
-
-
Please follow below code:
<?xml version="1.0" encoding="UTF-8"?>
<layout>
<default>
<reference name="root">
<block type="core/text_list" name="newreference" as="newreference" translate="label">
<label>New Reference</label>
</block>
</reference>
<reference name="newreference">
<block type="core/template" name="newreferenceblock" template="newreference.phtml" />
</reference>
</default>
</layout>
Reference: Add a New Reference in Magento
In latest versions you could use the footer_before position for this.
<default>
<reference name="footer_before">
<block type="core/template" name="customblock" template="customblock.phtml" />
</reference>
</default>