1

I have a setup where I am running a multi store system but I need different frontpages for every store. So I got my phtml-template sitting around and getting dynamically filled.

Now I added three more blocks (left, middle, right) on the frontpage and want to use the static block title as a heading for those blocks. (Maybe using the block title isn´t smart at all, I don´t know, guide me. :-))

When I am using the following code, I have output from different stores in place of my title:

<div class="grid_3">
  <span>
      <?php echo Mage::getModel('cms/block')->load('startseite_links')->getTitle() ?>
  </span>
  <div>
      <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('startseite_links')->toHtml() ?>
  </div>         
</div>

I wondered if there is another (simple) way to output the block title to use it as a heading. Whenever I search for outputting the block title I just stumble upon the getModel()-approach.

Any other ways to go? Or better drop block titles as headings?

2
  • Why do you want this? If this is for performance reasons then don't worry too much. MySQL's query cache will handle this. You're doing the exact same query (select * from cms_block where identifier = "startseite_links") twice in a row.
    – 7ochem
    Commented Feb 8, 2016 at 11:47
  • No, the problem is, that Store View C shows the static block titles from Store View A. I have tried disabled caching and of course my blocks are limited to their store views, but somehow the information "swaps over". I thought this might be a result of the getModel() thing.
    – flomei
    Commented Feb 8, 2016 at 12:16

3 Answers 3

1

The only other approach I can see is using a collection instead of load the entire CMS block (but as this model is quite small in terms of data I don't think it will save a lot of resources).

Instead of:

<div class="grid_3">
  <span>
      <?php echo Mage::getModel('cms/block')->load('startseite_links')->getTitle() ?>
  </span>
  <div>
      <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('startseite_links')->toHtml() ?>
  </div>         
</div>

I would do :

<div class="grid_3">
  <span>
      <?php 
      $blockId = 'startseite_links';
      $collection = Mage::getResourceModel('cms/block_collection')
                          ->addFieldToSelect('title')
                          ->addFieldToFilter('block_id', $blockId)
                          ->setPageSize(1);
      echo $collection->getFirstItem()->getTitle() ?>
  </span>
  <div>
      <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId($blockId)->toHtml() ?>
  </div>         
</div>
1

If anyone should stumble over this, I have found a solution for my problem.

If you don´t specifically retrieve a block for your store view, based on its ID, Magento seems to just return the first or any block with that identifier it can find.

The solution looks like this now:

Retrieving block content

<?php 

$cms_block = Mage::getModel('cms/block')
             ->setStoreId( Mage::app()->getStore()->getId() )
             ->load('my_block_identifier');

if($cms_block->getIsActive()) {
        echo $this->getLayout()->createBlock('cms/block')
             ->setBlockId('my_block_identifier')->toHtml();
}

?>

Retrieving block title

<?php 

$cms_block = Mage::getModel('cms/block')
             ->setStoreId( Mage::app()->getStore()->getId() )
             ->load('my_block_identifier');

if($cms_block->getIsActive()) { echo $cms_block->getTitle(); }

?>
1

Just use the model singleton like this:

$sizeChartBlock = Mage::getModel('cms/block')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->load('some-id-here');

And the object in $sizeChartBlock will have 2 methods with data to use in your template:

  1. getTitle
  2. getContent

You can easily do something like this:

<h2><?php echo $sizeChartBlock->getTitle(); ?></h2>
<?php echo $sizeChartBlock->getContent(); ?>

And there is no need to call it twice. Hope it helps.

Your Answer

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

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