{"id":17990,"date":"2018-12-20T12:44:11","date_gmt":"2018-12-20T10:44:11","guid":{"rendered":"https:\/\/belvg.com\/blog\/?p=17990"},"modified":"2023-10-13T15:53:24","modified_gmt":"2023-10-13T13:53:24","slug":"how-to-use-blocks-in-magento-2-development","status":"publish","type":"post","link":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html","title":{"rendered":"How to Use Blocks in Magento 2 Development"},"content":{"rendered":"\n<p>This article is dedicated to a very important aspect from Backend Certification&nbsp; &ndash; How to use blocks in&nbsp;Magento 2, which is the part of one large topic&nbsp;3 &ldquo;Customizing the Magento UI&rdquo;. Today we will consider the following questions:<!--more--><\/p>\n<p><a href=\"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2018\/12\/backround-8-1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy aligncenter size-full wp-image-17994 background\" src=\"https:\/\/belvg.com\/wp-content\/uploads\/2019\/06\/pixel.png\" data-src=\"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2018\/12\/backround-8-1.png\" alt=\"backround 8 1\" width=\"1110\" height=\"300\" title=\"How to Use Blocks in Magento 2 Development\"><\/a><\/p>\n<p><a href=\"#step_1\">What is&nbsp;block architecture and how is it used in development?<br>\n<\/a><a href=\"#step_2\">What are the stages in lifecycle of a block?<br>\n<\/a><a href=\"#step_3\">How are blocks rendered and cached?<\/a><\/p>\n<h2 id=\"step_1\" class=\"white\">What is&nbsp;block architecture and how is it used in development<\/h2>\n<h3>What is the typical block&rsquo;s role?<\/h3>\n<p>Separation block-template allows you to separate business logic from design..Blocks are usually (but not always) connected to PHTML template files. Blocks can be thought of as a data container for a template, which represents a piece of the HTML on the page. In layouts XML, you can manage blocks on the page and add new ones, set templates to move and delete.<\/p>\n<h3><strong>Which objects are accessible from the block?<\/strong><\/h3>\n<p><span style=\"font-weight: 400\">All block extends <\/span><span style=\"font-weight: 400\">\\Magento\\Framework\\View\\Element\\Template<\/span><span style=\"font-weight: 400\">, so by default blocks are injected with a <\/span><i><span style=\"font-weight: 400\">\\Magento\\Framework\\View\\ Element\\Template\\Context<\/span><\/i><span style=\"font-weight: 400\"> object and support getters and setters for returning and storage any objects or data. This object contains a number of other objects that are loaded into the instance:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><b><i>$this-&gt;validator<\/i><\/b><span style=\"font-weight: 400\">: <\/span><span style=\"font-weight: 400\">\\Magento\\Framework\\View\\Element\\Template\\File\\Validator<\/span><\/li>\n<li style=\"font-weight: 400\"><b><i>$this-&gt;resolver<\/i><\/b><span style=\"font-weight: 400\">: \\Magento\\Framework\\View\\Element\\Template\\File\\Resolver <\/span><\/li>\n<li style=\"font-weight: 400\"><b><i>$this-&gt;_filesystem<\/i><\/b><span style=\"font-weight: 400\">: <\/span><span style=\"font-weight: 400\">\\Magento\\Framework\\Filesystem <\/span><\/li>\n<li style=\"font-weight: 400\"><b><i>$this-&gt;templateEnginePool<\/i><\/b><span style=\"font-weight: 400\">: <\/span><span style=\"font-weight: 400\">\\Magento\\Framework\\View\\TemplateEnginePool<\/span><\/li>\n<li style=\"font-weight: 400\"><b><i>$this-&gt;_storeManager<\/i><\/b><span style=\"font-weight: 400\">:<\/span><span style=\"font-weight: 400\"> \\Magento\\Store\\Model\\StoreManagerInterface<\/span><\/li>\n<li style=\"font-weight: 400\"><b><i>$this-&gt;_appState<\/i><\/b><span style=\"font-weight: 400\">: <\/span><span style=\"font-weight: 400\">\\Magento\\Framework\\App\\State <\/span><\/li>\n<li style=\"font-weight: 400\"><b><i>$this-&gt;pageConfig<\/i><\/b><span style=\"font-weight: 400\">: <\/span><span style=\"font-weight: 400\">\\Magento\\Framework\\View\\Page\\Config<\/span><\/li>\n<\/ul>\n<h2 id=\"step_2\" class=\"white\">What are the stages in lifecycle of a block?<\/h2>\n<p><span style=\"font-weight: 400\">There are two phases of the block life cycle: <\/span><b><i>generating <\/i><\/b><span style=\"font-weight: 400\">blocks and <\/span><b><i>rendering <\/i><\/b><span style=\"font-weight: 400\">blocks. Blocks are instantiated at the moment the layout is created. They are not executed at that time, just instantiated. Also during this phase, the structure is built, the children of blocks are set, and for each block, the <\/span><b><i>prepareLayout()<\/i><\/b><span style=\"font-weight: 400\"> method is called. &nbsp;<\/span><\/p>\n<p><span style=\"font-weight: 400\">However, nothing is rendered, as that happens in the later rendering phase.<\/span><\/p>\n<p><b>Generating<\/b><span style=\"font-weight: 400\"> phase:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Magento\\Framework\\View\\Page\\Config::<\/span><span style=\"font-weight: 400\">publicBuild()<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Magento\\Framework\\View\\Page\\Config::<\/span><span style=\"font-weight: 400\">build()<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Magento\\Framework\\View\\Layout\\Builder::<\/span><span style=\"font-weight: 400\">build()<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Magento\\Framework\\View\\Layout\\Builder::generateLayoutBlocks()<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Magento\\Framework\\View\\Layout::generateElements()<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Magento\\Framework\\View\\Layout\\GeneratorPool::<\/span><span style=\"font-weight: 400\">process()<\/span><\/li>\n<\/ol>\n<p><b><i>GeneratePool <\/i><\/b><span style=\"font-weight: 400\">goes through all generators and <\/span><span style=\"font-weight: 400\">generate all scheduled elements. It has generators with the following elements:<\/span><\/p>\n<p><span style=\"font-weight: 400\">Magento\\Framework\\View\\Page\\Config\\Generator\\Head<\/span><br>\nMagento\\Framework\\View\\Page\\Config\\Generator\\Body<br>\n<span style=\"font-weight: 400\">Magento\\Framework\\View\\Layout\\Generator\\Block<\/span><br>\nMagento\\Framework\\View\\Layout\\Generator\\Container<br>\n<span style=\"font-weight: 400\">Magento\\Framework\\View\\Layout\\Generator\\UiComponent <\/span><\/p>\n<p><b>Rendering <\/b><span style=\"font-weight: 400\">phase:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Magento\\Framework\\View\\<\/span><span style=\"font-weight: 400\">Result\/Page::render()<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Magento\\Framework\\View\\Layout::getOutput()<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400\">At this moment, we already have all the layout xml for the page generated, and all block classes are created. <\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Magento\\Framework\\View\\Layout::<\/span><span style=\"font-weight: 400\">renderElement<\/span><span style=\"font-weight: 400\">()<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Magento\\Framework\\View\\Layout::<\/span><span style=\"font-weight: 400\">renderNonCachedElement<\/span><span style=\"font-weight: 400\">()<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400\">In this method we check type of rendered elements. It creates them and returns html using <\/span><b>toHtml()<\/b><span style=\"font-weight: 400\"> method. This method is not recommended to override. If you want to change block rendering override<\/span><b> _toHtml()<\/b><span style=\"font-weight: 400\"> method.<\/span><\/p>\n<h3>In what cases would you put your code in the _prepareLayout(), _beforeToHtml(), and _toHtml() methods?<\/h3>\n<p><b>_prepareLayout()<\/b><span style=\"font-weight: 400\"> &ndash; most often used for:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">adding child&rsquo;s <\/span><span style=\"font-weight: 400\">Magento\\Eav\\Block\\Adminhtml\\Attribute\\Edit\\Options\\AbstractOptions::_prepareLayout()<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">adding <\/span><span style=\"font-weight: 400\">tabs on backend <\/span><span style=\"font-weight: 400\">Magento\\Backend\\Block\\System\\Design\\Edit\\Tabs::_prepareLayout()<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">set title <\/span><span style=\"font-weight: 400\">Magento\\Wishlist\\Block\\Customer\\Sharing::_prepareLayout()<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">adding <\/span><span style=\"font-weight: 400\">pager, breadcrumbs and so on <\/span><span style=\"font-weight: 400\">Magento\\Sales\\Block\\Order\\History::_prepareLayout()<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">set renderer <\/span><span style=\"font-weight: 400\">Magento\\Catalog\\Block\\Adminhtml\\Form::_prepareLayout()<\/span><\/li>\n<\/ul>\n<p><b>_beforeToHtml() <\/b><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">data preparing <\/span><span style=\"font-weight: 400\">Magento\\Catalog\\Block\\Product\\ProductList\\Related::_beforeToHtml()<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">assign values <\/span><span style=\"font-weight: 400\">Magento\\Backend\\Block\\Widget\\Form\\Element::_beforeToHtml()<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">adding child&rsquo;s &nbsp;<\/span><span style=\"font-weight: 400\">Magento\\Shipping\\Block\\Adminhtml\\Create\\Items::_beforeToHtml()<\/span><\/li>\n<\/ul>\n<p><b>_toHtml() <\/b><\/p>\n<p><span style=\"font-weight: 400\"> Using this method, you can manipulate the rendering of the block, complement the condition, make wrappers for html, change the template for the block etc.<\/span><span style=\"font-weight: 400\">&nbsp;Magento\\GroupedProduct\\Block\\Order\\Email\\Items\\Order\\Grouped::_toHtml()<\/span><span style=\"font-weight: 400\"> , <\/span><span style=\"font-weight: 400\">M<\/span><span style=\"font-weight: 400\">agento\\Sales\\Block\\Reorder\\Sidebar::_toHtml()<\/span><\/p>\n<h3>How would you use events fired in the abstract block?<\/h3>\n<p><span style=\"font-weight: 400\">view_block_abstract_to_html_before<\/span><span style=\"font-weight: 400\"> &ndash; use for editing params:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">templates<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">caches<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">grids<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">view_block_abstract_to_html_after<\/span><span style=\"font-weight: 400\"> &ndash; use for <\/span><span style=\"font-weight: 400\">html manipulation &ndash; editing,add conditions, wrappers.<\/span><\/p>\n<h2 id=\"step_3\" class=\"white\">How are blocks rendered and cached?<\/h2>\n<p><span style=\"font-weight: 400\">The most important method for rendering a block is <\/span><span style=\"font-weight: 400\">Magento\\Framework\\View\\Element\\AbstractBlock::toHtml()<\/span><span style=\"font-weight: 400\">. He first run <\/span><span style=\"font-weight: 400\">_loadCache()<\/span><span style=\"font-weight: 400\"> , if the cache is missing then it run <\/span><span style=\"font-weight: 400\">_beforeToHtml()<\/span><span style=\"font-weight: 400\"> after the block is rendered by the method<\/span><span style=\"font-weight: 400\">_toHtml()<\/span><span style=\"font-weight: 400\"> after which the cache is saved <\/span><span style=\"font-weight: 400\">_saveCache($html)<\/span><span style=\"font-weight: 400\"> and run <\/span><span style=\"font-weight: 400\">_afterToHtml($html)<\/span><span style=\"font-weight: 400\">.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Method <\/span><span style=\"font-weight: 400\">_loadCache()<\/span><span style=\"font-weight: 400\"> uses <\/span><b><i>cache_key <\/i><\/b><span style=\"font-weight: 400\">return by <\/span><span style=\"font-weight: 400\">getCacheKey()<\/span><span style=\"font-weight: 400\"> &nbsp;and &nbsp;<\/span><span style=\"font-weight: 400\">_saveCache($html)<\/span><span style=\"font-weight: 400\"> &nbsp;&ndash; <\/span><b><i>cache_tags <\/i><\/b><span style=\"font-weight: 400\">obtained by the method <\/span><span style=\"font-weight: 400\">getCacheTags()<\/span><span style=\"font-weight: 400\">. <\/span><b>Cache_key <\/b><span style=\"font-weight: 400\">each block is added up like <\/span><span style=\"font-weight: 400\">BLOCK_<\/span><b><i>$<\/i><\/b><b><i>cache_key , <\/i><\/b><span style=\"font-weight: 400\">if this property not defined &ndash; BLOCK_sha1(<\/span><b><i>$name_in_layout<\/i><\/b><span style=\"font-weight: 400\">). <\/span><b>Cache_tags <\/b><span style=\"font-weight: 400\">is array and consists of a property <\/span><b><i>$<\/i><\/b><b><i>cache_tags<\/i><\/b><span style=\"font-weight: 400\">, if it defined in block and if block <\/span><span style=\"font-weight: 400\">instanceof <\/span><span style=\"font-weight: 400\">&nbsp;<\/span><span style=\"font-weight: 400\">Magento\\Framework\\DataObject\\IdentityInterface<\/span> <span style=\"font-weight: 400\">values returned by the method <\/span><span style=\"font-weight: 400\">getIdentities()<\/span><span style=\"font-weight: 400\"> are added to the array. We can manage <\/span><b><i>$cache_lifetime <\/i><\/b><span style=\"font-weight: 400\">variable. Value will be in seconds, if you want to disable cache you can set value to 0 or not set the value because all blocks are non-cacheable by default.<\/span><\/p>\n<h3>What are&nbsp;different types of blocks used for?<\/h3>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Magento\\Framework\\View\\Element\\AbstractBlock<\/span><span style=\"font-weight: 400\"> &ndash; parent block for all custom blocks;<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Magento\\Framework\\View\\Element\\Template<\/span><span style=\"font-weight: 400\"> &ndash; <\/span><span style=\"font-weight: 400\">block with template;<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Magento\\Framework\\View\\Element\\Text<\/span><span style=\"font-weight: 400\"> &ndash; <\/span><span style=\"font-weight: 400\">just rendering text;<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Magento\\Framework\\View\\Element\\FormKey<\/span><span style=\"font-weight: 400\"> &ndash; <\/span><span style=\"font-weight: 400\">return hidden input with form key;<\/span><\/li>\n<li style=\"font-weight: 400\">Magento\\Framework\\View\\Element\\Messages &ndash; rendering notification message by type.<\/li>\n<\/ul>\n<h3>When would you use non-template block types?<\/h3>\n<p><span style=\"font-weight: 400\">When we use simple renderers. When block content is dynamic generated or stored in a database or in containers. For example, if you want form_key in template, you can insert block <\/span><span style=\"font-weight: 400\">Magento\\Framework\\View\\Element\\FormKey<\/span><span style=\"font-weight: 400\">.<\/span><\/p>\n<h3>In what situation should you use a template block or other block types?<\/h3>\n<p><span style=\"font-weight: 400\">When we use complicate renderers. When we want to customize themes or add new blocks. &#1040; prime example is a <\/span><span style=\"font-weight: 400\">breadcrumb block <\/span><span style=\"font-weight: 400\">Magento\\Catalog\\Block\\Breadcrumbs<\/span><span style=\"font-weight: 400\">. If you want to customize <\/span><span style=\"font-weight: 400\">breadcrumbs, you can set the template in <\/span><span style=\"font-weight: 400\">layout<\/span><span style=\"font-weight: 400\">.<\/span><\/p>\n<p>This is all to the topic of use of blocks in Magento 2. If you have any questions left or comments, please leave them down below.<\/p>\n\n","protected":false},"excerpt":{"rendered":"This article is dedicated to a very important aspect from Backend Certification&#160; &#8211; How to &#8230;","protected":false},"author":82,"featured_media":17993,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_acf_changed":false,"footnotes":""},"categories":[14,491,389,469,471,524],"tags":[],"class_list":["post-17990","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-magento","category-magento-2","category-magento2-development","category-magento2-tutorial","category-magento-developer","category-magento-speed-optimization","post_format-post-format-image"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.4 - aioseo.com -->\n\t<meta name=\"description\" content=\"Find out how to use blocks in Magento 2 development. Discover Magento block architecture, stages of block lifestyle and how to use different types of blocks.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Andrey Litvin\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.4\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-use-blocks-in-magento-2-development.html#article\",\"name\":\"How to Use Blocks in Magento 2 Development | BelVG Blog\",\"headline\":\"How to Use Blocks in Magento 2 Development\",\"author\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrey-litvin#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/12\\\/How-to-Use-Blocks-in-Magento-2-Development.png\",\"width\":1110,\"height\":590,\"caption\":\"Magento 2 block usage\"},\"datePublished\":\"2018-12-20T12:44:11+02:00\",\"dateModified\":\"2023-10-13T15:53:24+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":6,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-use-blocks-in-magento-2-development.html#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-use-blocks-in-magento-2-development.html#webpage\"},\"articleSection\":\"Magento, Magento 2, Magento 2 Development, Magento 2 Tutorial, Magento Developer, Magento Speed Optimization, Image\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-use-blocks-in-magento-2-development.html#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento#listItem\",\"name\":\"Magento\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento#listItem\",\"position\":2,\"name\":\"Magento\",\"item\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-use-blocks-in-magento-2-development.html#listItem\",\"name\":\"How to Use Blocks in Magento 2 Development\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-use-blocks-in-magento-2-development.html#listItem\",\"position\":3,\"name\":\"How to Use Blocks in Magento 2 Development\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento#listItem\",\"name\":\"Magento\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\",\"name\":\"BelVG Blog\",\"description\":\"Magento and Prestashop articles\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrey-litvin#author\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrey-litvin\",\"name\":\"Andrey Litvin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-use-blocks-in-magento-2-development.html#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/e1a6e3c80cb940fddb65016d94b015cd951b4a8a9cd7227565131f28ea95c7a0?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Andrey Litvin\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-use-blocks-in-magento-2-development.html#webpage\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-use-blocks-in-magento-2-development.html\",\"name\":\"How to Use Blocks in Magento 2 Development | BelVG Blog\",\"description\":\"Find out how to use blocks in Magento 2 development. Discover Magento block architecture, stages of block lifestyle and how to use different types of blocks.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-use-blocks-in-magento-2-development.html#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrey-litvin#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrey-litvin#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/12\\\/How-to-Use-Blocks-in-Magento-2-Development.png\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-use-blocks-in-magento-2-development.html\\\/#mainImage\",\"width\":1110,\"height\":590,\"caption\":\"Magento 2 block usage\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/how-to-use-blocks-in-magento-2-development.html#mainImage\"},\"datePublished\":\"2018-12-20T12:44:11+02:00\",\"dateModified\":\"2023-10-13T15:53:24+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\",\"name\":\"BelVG Blog\",\"description\":\"Magento and Prestashop articles\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"How to Use Blocks in Magento 2 Development | BelVG Blog","description":"Find out how to use blocks in Magento 2 development. Discover Magento block architecture, stages of block lifestyle and how to use different types of blocks.","canonical_url":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html#article","name":"How to Use Blocks in Magento 2 Development | BelVG Blog","headline":"How to Use Blocks in Magento 2 Development","author":{"@id":"https:\/\/belvg.com\/blog\/author\/andrey-litvin#author"},"publisher":{"@id":"https:\/\/belvg.com\/blog\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2018\/12\/How-to-Use-Blocks-in-Magento-2-Development.png","width":1110,"height":590,"caption":"Magento 2 block usage"},"datePublished":"2018-12-20T12:44:11+02:00","dateModified":"2023-10-13T15:53:24+02:00","inLanguage":"en-US","commentCount":6,"mainEntityOfPage":{"@id":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html#webpage"},"isPartOf":{"@id":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html#webpage"},"articleSection":"Magento, Magento 2, Magento 2 Development, Magento 2 Tutorial, Magento Developer, Magento Speed Optimization, Image"},{"@type":"BreadcrumbList","@id":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/#listItem","position":1,"name":"Home","item":"https:\/\/belvg.com\/blog\/","nextItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento#listItem","name":"Magento"}},{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento#listItem","position":2,"name":"Magento","item":"https:\/\/belvg.com\/blog\/category\/magento","nextItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html#listItem","name":"How to Use Blocks in Magento 2 Development"},"previousItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html#listItem","position":3,"name":"How to Use Blocks in Magento 2 Development","previousItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento#listItem","name":"Magento"}}]},{"@type":"Organization","@id":"https:\/\/belvg.com\/blog\/#organization","name":"BelVG Blog","description":"Magento and Prestashop articles","url":"https:\/\/belvg.com\/blog\/"},{"@type":"Person","@id":"https:\/\/belvg.com\/blog\/author\/andrey-litvin#author","url":"https:\/\/belvg.com\/blog\/author\/andrey-litvin","name":"Andrey Litvin","image":{"@type":"ImageObject","@id":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/e1a6e3c80cb940fddb65016d94b015cd951b4a8a9cd7227565131f28ea95c7a0?s=96&d=mm&r=g","width":96,"height":96,"caption":"Andrey Litvin"}},{"@type":"WebPage","@id":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html#webpage","url":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html","name":"How to Use Blocks in Magento 2 Development | BelVG Blog","description":"Find out how to use blocks in Magento 2 development. Discover Magento block architecture, stages of block lifestyle and how to use different types of blocks.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/belvg.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html#breadcrumblist"},"author":{"@id":"https:\/\/belvg.com\/blog\/author\/andrey-litvin#author"},"creator":{"@id":"https:\/\/belvg.com\/blog\/author\/andrey-litvin#author"},"image":{"@type":"ImageObject","url":"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2018\/12\/How-to-Use-Blocks-in-Magento-2-Development.png","@id":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html\/#mainImage","width":1110,"height":590,"caption":"Magento 2 block usage"},"primaryImageOfPage":{"@id":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html#mainImage"},"datePublished":"2018-12-20T12:44:11+02:00","dateModified":"2023-10-13T15:53:24+02:00"},{"@type":"WebSite","@id":"https:\/\/belvg.com\/blog\/#website","url":"https:\/\/belvg.com\/blog\/","name":"BelVG Blog","description":"Magento and Prestashop articles","inLanguage":"en-US","publisher":{"@id":"https:\/\/belvg.com\/blog\/#organization"}}]}},"aioseo_meta_data":{"post_id":"17990","title":"How to Use Blocks in Magento 2 Development | #site_title","description":"Find out how to use blocks in Magento 2 development. Discover Magento block architecture, stages of block lifestyle and how to use different types of blocks.","keywords":null,"keyphrases":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_custom_url":null,"og_image_custom_fields":null,"og_image_url":null,"og_image_width":null,"og_image_height":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_image_url":null,"twitter_title":null,"twitter_description":null,"schema_type":"default","schema_type_options":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null,"created":"2026-11-03 18:36:58","updated":"2026-11-03 18:36:58"},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/belvg.com\/blog\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/belvg.com\/blog\/category\/magento\" title=\"Magento\">Magento<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tHow to Use Blocks in Magento 2 Development\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/belvg.com\/blog\/"},{"label":"Magento","link":"https:\/\/belvg.com\/blog\/category\/magento"},{"label":"How to Use Blocks in Magento 2 Development","link":"https:\/\/belvg.com\/blog\/how-to-use-blocks-in-magento-2-development.html"}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts\/17990","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/users\/82"}],"replies":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/comments?post=17990"}],"version-history":[{"count":3,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts\/17990\/revisions"}],"predecessor-version":[{"id":27495,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts\/17990\/revisions\/27495"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/media\/17993"}],"wp:attachment":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/media?parent=17990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/categories?post=17990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/tags?post=17990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}