CMS Blocks – Adding Block and Creating Unique Class ID

blockscmswidget

we add blocks to cms/blocks and pages using this convention

{{block type="cms/block" block_id="my_block1"}}
{{widget type="cms/widget_block" template="cms/widget/static_block/default.phtml" block_id="my_block2"}}

Now recently I had to specificall set the CSS styling to my_block1 (or my_block2 for that matter) when I saw that these blocks all have the same CSS CLASS widget widget-static-block. I was hoping to see something widget widget-static-block my_block1

My question: Now how can we add the block ID to the class? And would this not be a good idea to add to Magento core?

And as a side question: what exactly is the difference between the two cms/block calls? (1 widget and 1 block both under cms)

So the result would be something like widget widget-static-block my_block1 or cms_block my_block1

ps. I do understand there may be some caveats – because you might not want to always expose the exact block ID in your HTML. But I can imagine that for instance the name="" or as="" statement could in that case function as the name and it can be passed via the cms/block call and added as a CLASS element.

Best Answer

You can copy the template cms/widget/static_block/default.phtml to your theme and replace the original content

<div class="widget widget-static-block"><?php echo $this->getText(); ?></div>

with

<div class="widget widget-static-block <?php echo $this->getBlockId();?>"><?php echo $this->getText(); ?></div>

As for your side question, the only difference is that you can specify a template when using the widget approach. So you can have some kind of markup around the block text. Just like the template used in your example.