Computer's Guy
Computer's Guy

Reputation: 5363

Add block before content in magento page

I need to add a block right after the breadcrumbs in one of my pages, my-page

I tried creating a block called my-block in the admin, and adding <?php echo $this->getChildHtml('my-block') ?> in frontend/theme/default/template/page/2columns-left.phtml and it doesn't show the contents of the block my-block.

I also added

<page-name>
    <reference name="root">
        <block type="core/template" name="my-block" as="my-block" before="content" after="breadcrumbs" template="page/html/my-block.phtml"/>
    </reference>
</page-name>

In the local.xml file, but I'm not sure if it was really necessary.

Am I missing something?

thanks

Upvotes: 1

Views: 2593

Answers (1)

Vladimir Ramik
Vladimir Ramik

Reputation: 1930

Cms blocks have to be called differently to echo out their contents.

To render the block on all pages below breadcrumbs.phtml:

app\design\frontend\base\default\layout\page.xml

Add this after breadcrumbs:

In Layout.xml

<block type="cms/block" name="block_name">
    <action method="setBlockId"><block_id>my-block</block_id></action>
</block>

Then you'll have to echo it out in 3columns.phtml:

 <?php echo $this->getChildHtml('my-block') ?>

Might be easier to do it on a per-page basis.

Turn on template hints, find a block you want to append your cms content to and place the following in it:

In template file:

 // Insert the block into the page.
$sBlockId = 'my-block';
$oBlock = Mage::getModel( 'cms/block' );
$oBlock->setStoreId( Mage::app()->getStore()->getId() );
$oBlock->load( $sBlockId, 'identifier' );
$oCmsHelper = Mage::helper( 'cms' );
$oProcessor = $oCmsHelper->getPageTemplateProcessor();
$sHtml = $oProcessor->filter( $oBlock->getContent() );
echo $sHtml;

OR:

echo Mage::app()->getLayout()->createBlock( 'cms/block' )->setBlockId( 'my-block' )->toHtml();

Ideally you likely want to create a template that is then attached via layout.xml and inside that template you echo our your cms block.

Creating a template that renders your cms block wherever you desire:

app\etc\modules\Spirit_Cms.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Spirit_Cms>
            <active>true</active>
            <codePool>local</codePool>
        </Spirit_Cms>
    </modules>
</config>

app\code\local\Spirit\Cms\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Spirit_Cms>
            <version>0.0.1</version>
        </Spirit_Cms>
    </modules>
    <frontend>
        <layout>
            <updates>
                <spirit_cms>
                    <file>custom.xml</file>
                </spirit_cms>
            </updates>
        </layout>
    </frontend>
</config>

app\design\frontend\rwd\default\layout\custom.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="content">
            <block type="core/template" name="custom" template="custom.phtml" output="toHtml" />
        </reference>
    </default>
</layout>

app\design\frontend\rwd\default\template\custom.phtml

<?php echo Mage::app()->getLayout()->createBlock( 'cms/block' )->setBlockId( 'custom' )->toHtml(); ?>

More flexibility/less clutter adding content in a specified location.

Upvotes: 1

Related Questions