10

Is there a simple way as in magento1 :

 $crumbs = Mage::app()->getLayout->getBlock('breadcrumbs');
20

OUTDATED: ANSWER REFERS TO MAGENTO 2 ALPHA


I wish it was that easy.
It depends on where you want to instantiate it from.
If you want to create an instance from inside an other block do it like this:

$this->getLayout()->createBlock('Full\Block\Class\Name\Here');

From inside a controller do this:

 $this->_view->getLayout()->createBlock('Full\Block\Class\Name\Here');

From inside a model:

 $this->_blockFactory->createBlock('Full\Block\Class\Name\Here');

but here there is a catch.
you have to create a protected member on the model called _blockFactory and inject an instance of \Magento\Framework\View\Element\BlockFactory in the constructor and assign it to that member var.

Something like this:

protected $_blockFactory;
public function __construct(
   ...,
   \Magento\Framework\View\Element\BlockFactory $blockFactory,
   ....
){
    ....
    $this->_blockFactory = $blockFactory;
    ....
}

For instantiating a block from inside a helper it works the same as for model

  • the simpler the better – Matoeil Aug 21 '14 at 8:03
  • @Marius $toplink = $this->getLayout()->getBlock('top.links')->toHtml(); use in magento 1 for magento 2 $this->_view->getLayout()->getBlock(Full\Block\Class\Name\Here')->toHtml(); this give a error like this Fatal error: Call to a member function toHtml() on a non-object how can i get toHtml()? – ND17 Jan 28 '16 at 13:31
  • if you are in a controller, you don't need to inject anything for _view. It should be available by default, although I think it's deprecated now. I wrote this answer 1.5 years ago – Marius Jan 28 '16 at 13:36
  • 1
    @SachinS. You should not use that anymore. _view is deprecated now in controllers and it will soon go away, – Marius Sep 27 '16 at 7:36
  • 1
    Can i call using pagefactory object like this? $viewPanelPageFactory=$this->resultPageFactory->create(); $viewPanel= $viewPanelPageFactory->getLayout()->getBlock('catalog.leftnav')->toHtml(); – Sushivam Sep 27 '16 at 7:40
11

To instantiate a block you have to use \Magento\Framework\View\LayoutInterface class and its createBlock() method.

With an helper class you could do it with this code:

namespace Vendor\Module\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{

    /**
     * Layout
     *
     * @var \Magento\Framework\View\LayoutInterface
     */
     protected $_layout;


    /**
     *
     */
     public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\View\LayoutInterface $layout
     ) {
         $this->_layout = $layout;
         parent::__construct($context);
     }


    /**
     * Create new block
     */
     public function getBlock() {

        $block = $this->_layout
            ->createBlock('Magento\Framework\View\Element\Template')
            ->setTemplate('Vendor_Module::helper/block.phtml');

        return $block;

     }

}

Then call $helper->getBlock() from where you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.