Pergunta

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!

Foi útil?

Solução

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top