4

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

3

This is possible with CMS configuration only and no code:

  1. create a static block (CMS > Static Blocks) that contains your content

  2. Create a new widget instance (CMS > Widgets) and select your theme screenshot (2)

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

screenshot (3)4. Select static block from (1) in "Widget Options": screenshot (4)

  1. Save.

  2. 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);
}
| improve this answer | |
  • 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
  • addBlockAfterContent gives 505 error – inrsaurabh Oct 25 '17 at 5:43
  • @FabianSchmengler, where do I have to put the observer code addBlockAfterContent. In which file? – Black Jun 1 '18 at 11:36
1

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>
| improve this answer | |
  • I don't want to change any theme files. – Chirag Rajput Aug 24 '16 at 11:12
  • 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
  • Check improved answer – Ashish Jagnani Aug 24 '16 at 12:04
  • I put above code in my module's layout file but not working. – Chirag Rajput Aug 24 '16 at 12:47
  • Pl check the improved answer with block type="core/template" – Ashish Jagnani Aug 24 '16 at 13:24
0

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

| improve this answer | |
  • is it possible to create a new reference using a custom module – inrsaurabh Oct 25 '17 at 4:33
0

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>
| improve this answer | |
  • I already try it. Its not working :( – Chirag Rajput Aug 24 '16 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.