/   /   /  Magento 2 Layouts and blocks inside php

Note:

For more extensions and themes visit our store

Magento 2 Layouts and blocks inside php


Magento 2 have a good system for  organize the blocks by layouts, In Magento 2 that is more flexible . You can play by many ways .

We want show how the developers can play with all puzzles (layout block template and structure), the framwork Magento 2 have a component called View ("vendor/magento/framework/View"), it have almost all needed class for manage the frontend view in back and font office ,
 you can inject the class Magento\Framework\View\Layout  by factory for get instance of layout class . let's imagine your contruct  in plugin by example is :

  1.  
  2.     public function __construct(
  3.       \Magento\Framework\View\LayoutFactory $layoutFactory
  4.     ){
  5.         $this->_layoutFactory= $layoutFactory;
  6.     }
  7.  

And use in your fucntion :

  1.  
  2. public function resolveLayot()
  3. {
  4.         $this->_layoutFactory
  5.             ->createBlock('\MyVendor\MyModule\Block\Ship',
  6.             'mycustom.block',
  7.             ['data' => ['title' => "My Title"]]) ;
  8. }
  9.  

Let's translate all this lines to xml :

  1.  
  2. <block class="MyVendor\MyModule\Block\Ship" name="mycustom.block">
  3.         <arguments>
  4.         <argument translate="true" name="title" xsi:type="string">My Title</argument>
  5.         </arguments>
  6. </block>
  7.  

You can add the template .phtml path just push inside your block  MyVendor\MyModule\Block\Ship  the parameters $template as :

  1.  
  2. protected $_template = 'MyVendor_MyModule::template.phtml';
  3.  

But here is absolute declartion .  how you can tell to system where is the parent block or container ? Simple your should use the class structure Magento\Framework\View\Layout\Data\Structure inject it and you can use :

  1.  
  2. $this->structure->setAsChild('mycustom.block', 'product.info.details') ;
  3.  

now you can call as child inside details.phtml for example .

Or you can use directly the object  $this->_layoutFactory   and use the method setChild($parentName, $elementName, $alias) . You can  use a lot of methods inside class layout getBlock($name) getXml() addContainer($name, $label, array $options = [], $parent = '', $alias = '') .

So other information:  "the class Structure register all map of child parent , and type of element block or container , and class layout .  use it for determinete the type of element with method isContainer($name) isBlock($name)" .

Comments

Related Posts

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.