Following are the 4 conditions.
Also not sure about below conditions too.
Can anyone explain the above conditions and deduct whether these are true or not?
This question has also reported on Magento stack exchange :
https://magento.stackexchange.com/questions/276828/magento-2-blocks-and-templates
Hello @Kapil_Thakur
I never seen anyone using multiple block for single view but also I don't prefer to use multiple Block for one view as a block class itself is a huge Resource HOG , most Block classes that exist in Magento 2 are created by extending upon the \Magento\Framework\View\Element\Template class. This parent class brings template functionality, plus a lot of other handy things like references to the current layout, a logger and so on.
Here is where the ViewModel emerges with its true power. The concept is simple, we can inject any number of viewModel layers into a block as an argument and thus can use them in the presentation level. The glory resides in the fact that we can completely rid off the whole hell of dependency of the core class Mage\Core\Block\Template inside a viewModel.
__construct of the ViewModel only holds those dependencies which require to prepare the data for the presentational phtml file. This makes it cleaner and more reliable solution to do customization in the presentational level. Since the __constuct of a ViewModel does not want to deal with unwanted parental dependencies, this will eventually lead to the performance boost.
The most important part of using ViewModel is it simplifies the load time as we only implements Magento\Framework\View\Element\Block\ArgumentInterface. If we use a custom block instead of ViewModel, we should extend the core block Magento\Framework\View\Element\Template which includes so many handy things like some extra blocks, loggers etc. And we can easily access the ViewModel by $block->getViewModel().
Regards
Gaurav
I appreciate your answer, but my question is not about what are other ways to do the things,
I just want to know that the given 4 points are true or false and why?
If your or anyone can help on these four points.
Thanks !
Assuming this as a Product List Block for the followings :
Case 1: Single Block & Multiple Templates
No this can not be true as at a time only 1 template can be assigned to any block. you can not assign multiple templates to any block. the block template is assigned when its instance is created. so only 1 template can be assigned to block, Yes we can apply conditional statements and update the templates like in widgets in the admin area but the template will be one for any block.
Case 2: Multiple blocks and single template
Yes, this can be done for any new blocks that are having the same Product list page structure.
Case 3: Multiple block instances and single template
Yes, this is true, When any Block's instance is created than template is assigned to that block & for multiple instances, one template can be used.
Case 4: Multiple block instances and multiple templates
Yes, this is true, When any Block's instance is created then the template can be assigned via conditional statements within _toHtml() function like below :
/** * Returns block html * * @return string */ protected function _toHtml() { $type = $this->_request->getParam('type'); if ($type == 'new') { /* set new template if request parameter type = new */ $this->setTemplate('Magento_Catalog::product/list_new.phtml'); } return parent::_toHtml(); }
I hope this resolves your query!