1

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.

4

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.

1

Well, if you ask about a functional difference I think the answer should be clear. Blocks, and this is the same as it was in Magento1, have always a concrete block class associated. On the other hand, containers can't have any specific class associated, as they seem to have been created in Magento2 just as some wrappers of page content / sections

https://devdocs.magento.com/guides/v2.3/frontend-dev-guide/layouts/layout-overview.html

In Magento, the basic components of page design are layouts, containers, and blocks. A layout represents the structure of a web page (1). Containers represent the placeholders within that web page structure (2). And blocks represent the UI controls or components within the container placeholders (3). These terms are illustrated and defined below.

enter image description here

  • No :) , I mean which type of classes are extended by block and container. – Aditya Shah Jan 2 at 14:45
  • Don't understand what you are asking then :) – Raul Sanchez Jan 2 at 15:00
  • Updated the question :) – Aditya Shah Jan 3 at 5:40
1

I think Block and Container are different entity so there is no need to compare each other.

But here are some functional similarity between each-other.

For Ex: If you want to Extend (change the default behavior) of Block & Container you can use like this.

<referenceContainer name="content"></referenceContainer>

And

<referenceBlock name="product.info.addtocart"></referenceBlock>

A block can have the following features:

  • A block can contain other blocks.
  • A block can be used in several pages and blocks.

Example

A block Magento\Ui\Test\Block\Messages extends a basic block Magento\Mtf\Block\Block and implements methods to interact with messages.

Magento contains basic blocks for the functional testing with a logic that you can reuse. The most popular are the following:

  • Magento\Mtf\Block\Block
  • Magento\Mtf\Block\Form
  • Magento\Backend\Test\Block\Widget\Tab
  • Magento\Backend\Test\Block\Widget\FormTabs
  • Magento\Backend\Test\Block\Widget\Grid
  • Magento\Ui\Test\Block\Adminhtml\DataGrid

For better understand go through below reference link.

I hope it helps!

0

Both containers and blocks are basic components of the Magento page structure. Containers make up the framework of a page and they can be either empty or contain child elements: it could be blocks and other containers. An example of the container and blocks:

<container name="category.view.container" htmlTag="div" htmlClass="category-view" after="-">
<block class="Magento\Catalog\Block\Category\View" name="category.image" template="Magento_Catalog::category/image.phtml"/>
<block class="Magento\Catalog\Block\Category\View" name="category.description" template="Magento_Catalog::category/description.phtml"/>
<block class="Magento\Catalog\Block\Category\View" name="category.cms" template="Magento_Catalog::category/cms.phtml"/>
</container>

According to your question about child elements there is a Before/after attribute both for container and blocks. This attribute sets the order in which blocks and containers are displayed on a page.

More about containers and blocks you can read here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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