简体   繁体   中英

Magento - Accessing php block class from template phtml file

I came across a way of accessing BLOCK classes (from a module)...in a phtml file. I could basically use '$var = Mage::blahblah....' and get access to any Block file in the module.

I didnt save this stmt, now i need to use it again, and cant find the info i need.

Does anyone know what the statment would be?

many thanks

Block class instances can be instantiated anywhere like so:

Mage::app()->getLayout()->createBlock('core/template')->...;
//or $this->getLayout()->createBlock('core/template')->...;
//or $this->getLayout()->getBlock('block_name_loaded_in_controller_or_layout_or...');

To further Ben's response, I've used:

$cmsBlock = Mage::getModel('cms/block')->load($blockId)->getContent();
if($cmsBlock) {
    ...do stuff...
}

when making a check to see if a specific static block exists. This can then be echoed as normal to display the content.

The main difference here is that:

Mage::app()->getLayout()->createBlock('cms/block')->setBlockId($blockId)->toHtml();

Creates a new block but it may be empty if $blockId is not valid. The first method calls that specific static block. There may be another way here, but this is what worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM