5

Following are the 4 conditions.

  1. Single block and multiple templates - I think this is possible and we use it frequently while customizing Magento.
  2. Multiple blocks and single template - Never seen this one though.

Also not sure about below conditions too.

  1. Multiple block instances and single template
  2. Multiple block instances and multiple templates

Can anyone explain the above conditions and deduct whether these are true or not?

1
  • HungryDB did you got the solution ! because I am also confused in instances and block . Commented Mar 4, 2020 at 3:06

2 Answers 2

3

Source of answer https://community.magento.com/t5/Magento-2-x-Programming/Relationship-in-block-and-template/td-p/459182


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 the 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 then a 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();
}
0
  • Single block and multiple templates: allows to implement different design / themes yet using same codebase (useful for multi stores environment)

  • multiple blocks is not something I have used. However, assuming you may enforce these blocks to implement a contract using an interface, then the template would in that case work with all these blocks.

  • for 3: I suggest this is same as 2 but the block instance is just the object that exists when the code is executed whereas 2 suggests we are not executing any code

  • 4: looks quite theoretical to me. I’d say this condition is wrong or at least needs clarification as to the meaning/context of this question

4
  • This was the question in the M2 certification exam. One of 4 should be false. I don't know why they put the block and block instance conditions separately in question.
    – HungryDB
    Commented May 31, 2019 at 10:27
  • thanks, did not know that was the case.. Commented May 31, 2019 at 10:33
  • @Marius - Can you please confirm which is one is answer in this post ?
    – Mak David
    Commented Jan 17, 2020 at 18:27
  • is there any update in this question? Commented Mar 4, 2020 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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