0

Hi to all Developers out there!!!

I am using the below commands in footer.phtml in order to fetch all my cms/blocks in the magento's footer

 <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('home')->toHtml();?>


<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('contact')->toHtml();?>


<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('blog')->toHtml();?>

I wonder if I could find somekind of a loop in order to fetch all my blocks ( home, contact , blog etc,etc) in order to avoid repeating the above code...

Any suggestion ?

kost
  • 325
  • 1
  • 9
  • 29

3 Answers3

2

If what you're after is pure code reuse, something like this will work

<?php $_blocks = array('home','contact','block'); ?>
<?php foreach($_blocks as $_blockName): ?>
    <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId($_blockName)->toHtml();?>
<?php endforeach; ?>

Just add new block names to the $_blocks array.

You could also do this for all your blocks with something like this

    <?php $_blocks = Mage::getModel('cms/block')->getCollection(); ?>
    <?php foreach($_blocks as $_block): ?>
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId($_block->getIdentifier())->toHtml();?>
    <?php endforeach; ?>

but as other have mentioned, that seems like a bad idea w/r/t new blocks added to the system.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
1

This post may help: Magento - How do you return results of unlimited CMS Static blocks (with certain "Identifier") to a CMS Page

You can use collections in order to get all the cms blocks and you can also filter them.

Community
  • 1
  • 1
Karl
  • 5,435
  • 11
  • 44
  • 70
0

You can't. Or you must write your own function for it.

Impression
  • 46
  • 2