Magento 2 – How to Call Other Blocks Method in Templates

blocksmagento2template

We can define templates and block in layout file, after that we can call block methods in that template file.

My question is

Is there any way to call other block methods in phtml file(template file)?

Best Answer

You can call otherblock method inside any template file using below code,

You can pass here your block, instead of Magento\Catalog\Block\Product\ListProduct

      $imageBlock =  $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct');
      $product=$this->getProducts();
      $imageBlock->getImage($product, 'category_page_list');