Magento createBlock method does not work, displays static block data

Okay, that’s why Ive created static blocks in my CMS area and Im trying to output them inside Ive’s custom homepage template.

Each document I can find says to output a block as follows

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

This did not work for me, so I tried a different way. -

       <?php $block = Mage::getSingleton('core/layout')->createBlock('cms/block')->setBlockId('my-block-identifier');
        echo $block->toHtml();

All sites linking to this tell me to use the actual block id to get the block. So, I decided to manually find the block_id in my cms_block table and see if it works with the block_id number instead of the literal name my-block-identifier, and he did it. So I'm confused ... Can someone tell me how I can get the block by actual identifier or look for the block identifier by identifier so that I can capture the block by block name?

Any help is greatly appreciated.

+3
source share
2 answers

Looking at the source of the block cms/block, these guides mislead you or you misinterpreted them.

#File: app/code/core/Mage/Cms/Block/Block.php
class Mage_Cms_Block_Block extends Mage_Core_Block_Abstract
{
    protected function _toHtml()
    {
        if (!$this->_beforeToHtml()) {
            return '';
        }
        $html = '';
        if ($blockId = $this->getBlockId()) {
            $block = Mage::getModel('cms/block')
                ->setStoreId(Mage::app()->getStore()->getId())
                ->load($blockId);
            if (!$block->getIsActive()) {
                $html = '';
            } else {
                $content = $block->getContent();

                $processor = Mage::getModel('core/email_template_filter');
                $html = $processor->filter($content);
            }
        }
        return $html;
    }
}

->load($blockId); - load, loding .

, , , .

$model = Mage::getModel('cms/block')->getCollection()
->addFieldToFilter('identifier','footer_links')
->getFirstItem();

var_dump($model->getBlockId()); 
+1

, , Identifier second . . , contact-info , :

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

cms_block, .

, JD

0

Source: https://habr.com/ru/post/1764321/


All Articles