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 :
public function __construct( \Magento\Framework\View\LayoutFactory $layoutFactory ){ $this->_layoutFactory= $layoutFactory; }
And use in your fucntion :
public function resolveLayot() { $this->_layoutFactory ->createBlock('\MyVendor\MyModule\Block\Ship', 'mycustom.block', ['data' => ['title' => "My Title"]]) ; }
Let's translate all this lines to xml :
<block class="MyVendor\MyModule\Block\Ship" name="mycustom.block"> <arguments> <argument translate="true" name="title" xsi:type="string">My Title</argument> </arguments> </block>
You can add the template .phtml path just push inside your block MyVendor\MyModule\Block\Ship the parameters $template as :
protected $_template = 'MyVendor_MyModule::template.phtml';
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 :
$this->structure->setAsChild('mycustom.block', 'product.info.details') ;
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