Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: get a static block as html in a phtml file

Tags:

php

magento

I have a static block called newest_product (with content) and I would like to display it on a .phtml file as html.

I've tried this code:

echo $this->getLayout()->createBlock('cms/block')->setBlockId('newest_product')->toHtml();  

But this nothing is being displayed.

Am I using the wrong code?

like image 395
iamjonesy Avatar asked Jan 12 '11 12:01

iamjonesy


People also ask

How do you call a static block on CMS page in Magento 2?

1. Navigate to Content > Elements > Widgets and press the Add Widget button. 2. Choose the CMS Static Block as a Type, set the Design Theme and press Continue.

How do I get CMS block data by identifier in Magento 2?

You can get CMS Static Blocks collection by an identifier in Magento 2 by calling BlockRepositoryInterface interface. Magento\Cms\Api\BlockRepositoryInterface used for getting CMS Static Blocks in Magento 2.

How do I override information Phtml file in Magento 2?

If you need to override the info template with your module or theme level, you can do it with ease. Template declaration will be found from the Block class, Magento\Sales\Block\Order\Info with protected $_template variable. Add the content from the Original template with your modified changes.

How can I call Phtml file in another Phtml file Magento 2?

Re: How to call phtml file in another phtml fileecho $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magestore_Webpos::login. phtml")->toHtml();


1 Answers

If you have created CMS block named 'block_identifier' from admin panel. Then following will be code to call them in .phtml

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml();  ?>  
like image 127
Suman-PHP4U Avatar answered Sep 23 '22 23:09

Suman-PHP4U