如何在Magento页脚中编写php循环?

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 ?

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

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.

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.