Magento – the functional difference of Block & Container in Magento 2

blockscontainersmagento2

Block vs Container functional difference.

I have followed this url but doesn't clear much for me.

Which type of classes are extended by block and container.

Note : I know how containers and Block with everything else all works.

I want to understand Magento classes which are extended by block and container that implements rendering of a particular block & container, And also How it handles rendering child elements during view output generation.

Best Answer

Container is a concept to create structure in both your layout and your html page: I had a play with containers before writing this post and below you can see an example how to use it:

<referenceContainer name="content">
   <container htmlId="mycontainer" name="mycontainer" htmlTag="ol">
      <block name="test" class="Magento\Framework\View\Element\Text">
         <arguments>
           <argument name="text" xsi:type="string"><![CDATA[<li>this is some text</li>]]></argument>
          </arguments>
      </block>
      <block name="testw" class="Magento\Framework\View\Element\Text">
          <arguments>
              <argument name="text" xsi:type="string"><![CDATA[<li>this is some other text</li>]]></argument>
          </arguments>
      </block>
   </container>

As you can see below, the container has no physical ties like a block: the block is having a Block class and a template

To a container, you can assign css class, html id, html tag type (see the example above). The container eventually sits as a html tag in your page and will render all its block and child containers.

Finally, both can be referenced in the layout in the same way and that is possibly where your confusion come from.