Static Block – Retrieving Title Without getModel()

static-block

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?

Best Answer

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>
Related Topic