8

I searched the web and could not find how to do this.

I want to add a static block that I have created in admin before to a cms-page. This works using either of this lines:

{{block type="cms/block" block_id="my_block_id"}}
{{block type="cms/block" block_id="my_block_id" template="cms/content.phtml"}}

Now I want to use a custom template, i.e.:

{{block type="cms/block" block_id="my_block_id" template="modulename/custom-template.phtml"}}

When I put it into the cms-page like this, the block is shown, but the "template"-tag is completely ignored - cms/content.phtml is used.

WHAT I TRIED
I tried to extend class Mage_Block_Cms_Block and add setTemplate($this->getTemplate()); to _toHtml()-function. The effect is the same as above - cms/content.phtml is used.

I tried to extend class Mage_Core_Block_Template; of course I can set a template here, but I have problems getting the static block. I can't find how to get the block by block-id.

WHAT THIS QUESTION IS ABOUT / NOT ABOUT
I know how to do this with PHP.
I know how to do this with XML-files.
It s crucial to this question that the blocks can be managed in the backend.

I run Magento CE 1.7.0.0.

Thank you for your time!

Marius
199k55 gold badges431 silver badges837 bronze badges
asked Apr 6, 2013 at 14:39

2 Answers 2

8

You cannot change the template for a static block because the static block does not have a template. Take a look at the method: Mage_Cms_Block_Block::_toHtml(). The _toHtml() method is used to render any block object, and in the case of the cms blocks it only renders the content of the block.

If you want to wrap the content of any cms block in some markup you can try this:

{{block type="core/template" template="custom/block.phtml" block_id="some_block_id"}}

And in the file custom/block.phtml do this:

<?php
$block = Mage::app()->getLayout()->createBlock('cms/block')->setBlockId($this->getBlockId()); //retrieve the cms block with the id set on this layout block
$html = $block->toHtml();//in this var you have the content of the cms block 
?>
<?php if ($html) : //this is needed to avoid additional markup if the cms block is empty?>
<div class="some-class">
 <div class="some-other-class">
 <?php echo $html;//actuall cms block?>
 </div>
</div>
<?php endif;?>

I hope this is what you needed.

answered Apr 8, 2013 at 9:26
2
  • 1
    Nice solution, never thought of it this way. Still, wouldn't creating a widget be more appropriate? Anyway +1 Commented Apr 8, 2013 at 9:53
  • I guess that it could work with a widget, but this means that all the cms blocks rendered via widget would have the same markup around it. This way you can have different templates for different static blocks. If you want all to be rendered the same I guess the widget approach should be enough. Commented Apr 8, 2013 at 10:34
3

Have you tried with cms/widget_block? This block extends from Mage_Core_Model_Template, so it might be possible to do what you are trying to do.

answered Apr 6, 2013 at 20:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.