{"id":282315,"date":"2021-02-06T18:29:13","date_gmt":"2021-02-06T18:29:13","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=282315"},"modified":"2025-01-21T08:05:51","modified_gmt":"2025-01-21T08:05:51","slug":"magento-2-layout-and-templates","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/","title":{"rendered":"Magento 2: Layout and Templates"},"content":{"rendered":"\n<p>Now let&#8217;s come back to frontend development. Earlier we created a controller where I wrote a &#8220;Hello World&#8221; statement to show you that the controller is working. But in <a href=\"https:\/\/business.adobe.com\/in\/products\/magento\/magento-commerce.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento<\/a>, we don&#8217;t write HTML code in the controller.<\/p>\n\n\n\n<p>To show HTML content we use .phtml files in Magento. These .phtml files are called template files. <\/p>\n\n\n\n<p>To map the controller with .phtml files we use layout files. The layout files are of .xml type. As all of these are related to viewing the page. So it makes sense to put them in a different folder, which is called the <strong>view<\/strong> folder.<\/p>\n\n\n\n<p>First, we need to modify the controller file, <strong>Controller\/Manage\/Add.php<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"331\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099-1200x331.png\" alt=\"Selection_099\" class=\"wp-image-390577\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099-1200x331.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099-300x83.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099-250x69.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099-768x212.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099.png 1517w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">File Structure<\/figcaption><\/figure>\n\n\n\n<p>Updated code for <em>Controller\/Manage\/Add.php<\/em> file<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace Webkul\\BlogManager\\Controller\\Manage;\n\nuse Magento\\Customer\\Controller\\AbstractAccount;\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\View\\Result\\PageFactory;\n\nclass Add extends AbstractAccount\n{\n    \/**\n     * @var PageFactory\n     *\/\n    private $resultPageFactory;\n\n    \/**\n     * Dependency Initilization\n     *\n     * @param Context $context\n     * @param PageFactory $resultPageFactory\n     *\/\n    public function __construct(\n        Context $context,\n\tPageFactory $resultPageFactory\n    ) {\n\t$this-&gt;resultPageFactory = $resultPageFactory;\n        parent::__construct($context);\n    }\n\n    \/**\n     * Provides content\n     *\n     * @return \\Magento\\Framework\\View\\Result\\Page\n     *\/\n    public function execute()\n    {\n        $resultPage = $this-&gt;resultPageFactory-&gt;create();\n        $resultPage-&gt;getConfig()-&gt;getTitle()-&gt;set(__(&#039;Add Blog&#039;));\n        $layout = $resultPage-&gt;getLayout();\n        return $resultPage;\n    }\n}<\/pre>\n\n\n\n<p>Here we are using <em>\\Magento\\Framework\\View\\Result\\PageFactory<\/em> class to initialize the layout. With this we can configure the page, here we have set the Title of the page. <\/p>\n\n\n\n<p>And after calling the <em>getLayout()<\/em> method to process the layout file, we returned the result page.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Layout<\/h2>\n\n\n\n<p>Now if you notice we have not explicitly mentioned the layout file name in the controller. So Magento will look for the layout file with the name <strong>routerId_controllerName_actionName.xml<\/strong>. If we want then we can explicitly give like,<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">$resultPage-&gt;addHandle(&#039;some_other_layout&#039;);<\/pre>\n\n\n\n<p>For the layout file, we need to create a view\/frontend\/layout folder under the module directory. <\/p>\n\n\n\n<p>In the layout folder, we need to create a layout file with the name blogmanager_manage_add.xml. If you remembered we used blogmanager as our router id.<\/p>\n\n\n\n<p>It&#8217;s common practice to use routerId as the same as frontName because then the layout file name will be similar to the URL.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"388\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_100-1200x388.png\" alt=\"Selection_100\" class=\"wp-image-390597\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_100-1200x388.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_100-300x97.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_100-250x81.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_100-768x248.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_100.png 1511w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">Folder Structure<\/figcaption><\/figure>\n\n\n\n<p>Code for <em>view\/frontend\/layout\/blogmanager_manage_add.xml<\/em> file<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;page xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; layout=&quot;1column&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd&quot;&gt;\n    &lt;body&gt;\n        &lt;referenceContainer name=&quot;content&quot;&gt;\n            &lt;block class=&quot;Magento\\Framework\\View\\Element\\Template&quot; name=&quot;blogmanager.blog.add&quot; template=&quot;Webkul_BlogManager::add.phtml&quot; \/&gt;\n        &lt;\/referenceContainer&gt;\n    &lt;\/body&gt;\n&lt;\/page&gt;<\/pre>\n\n\n\n<p>The body node refers to that we are modifying contents within the body tag of the HTML. The page layout in Magento is made out of multiple containers. <\/p>\n\n\n\n<p>And the container can have nested containers. The innermost containers have blocks in them and each block can have child blocks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Layout Types<\/h2>\n\n\n\n<p>As you can see in the page tag we have used the layout attribute with the value 1column. Magento supports different types of layouts such as,<br>i) 1coumn<br>ii) 2columns-left<br>iii) 2columns-right<br>iv) 3columns<\/p>\n\n\n\n<p>For simplicity, you can think of the header, footer, and main content as individual containers. However, Magento&#8217;s layout structure is much more complex, which helps us to modify any component.<\/p>\n\n\n\n<p>For the 2columns-left layout, the main content will have two sub-containers, one for content and another on the left side as a sidebar container.<\/p>\n\n\n\n<p>With the referenceContainer tag, we can target any container, here we are targeting the main container whose name is content.<\/p>\n\n\n\n<p>You can find more container and block names in the <em>vendor\/magento\/module-theme\/view\/frontend\/layout\/default.xml<\/em> file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"736\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_22-35-1200x736.png\" alt=\"2021-02-06_22-35\" class=\"wp-image-282324\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_22-35-1200x736.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_22-35-300x184.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_22-35-250x153.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_22-35-768x471.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_22-35.png 1432w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><\/figure>\n\n\n\n<p>Inside the container, we are calling our phtml file with a block tag. <\/p>\n\n\n\n<p>Here we are using the default template block class Magento\\Framework\\View\\Element\\Template but we can create our own block class if we have to pass some data into the phtml.<\/p>\n\n\n\n<p>We will see more about the Block class in the next blog. The block tag has a name attribute, we can use any sensible name here to identify that block.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Templates<\/h2>\n\n\n\n<p>The interesting part in the block tag is <strong>template=&#8221;Webkul_BlogManager::add.phtml&#8221;<\/strong>, this represents the path of the template file. <\/p>\n\n\n\n<p>The first part of :: is the module name and the second part is the path in reference to the <strong>view\/frontend\/templates<\/strong> folder.<\/p>\n\n\n\n<p>As in the path we have given the phtml file name so we can directly create add.phtml file under the <strong>view\/frontend\/templates<\/strong> folder.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"415\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_101-1200x415.png\" alt=\"Selection_101\" class=\"wp-image-390598\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_101-1200x415.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_101-300x104.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_101-250x86.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_101-768x265.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_101.png 1514w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">Folder Structure<\/figcaption><\/figure>\n\n\n\n<p>Code for <em>view\/frontend\/templates\/add.phtml<\/em> file<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;div class=&quot;blog-container&quot;&gt;\n    &lt;form class=&quot;blog-form&quot;&gt;\n        Title: &lt;input type=&quot;text&quot; name=&quot;title&quot;\/&gt;\n        Content: &lt;textarea name=&quot;content&quot;&gt;&lt;\/textarea&gt;\n        &lt;button type=&quot;submit&quot;&gt;Submit&lt;\/button&gt;\n    &lt;\/form&gt;\n&lt;\/div&gt;<\/pre>\n\n\n\n<p>After writing all of this code we have to run the di compile command. And now if you browse the URL you will see the page,<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"680\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_23-22-1200x680.png\" alt=\"2021-02-06_23-22\" class=\"wp-image-282325\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_23-22-1200x680.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_23-22-300x170.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_23-22-250x142.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_23-22-768x435.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_23-22-1536x870.png 1536w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_23-22.png 1857w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><\/figure>\n\n\n\n<p>Please try out different layout types on your own.<\/p>\n\n\n\n<p>If you make some changes in the template file and can not find it when reloading the page then please flush the cache.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">php bin\/magento cache:flush<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Enhancing<\/h2>\n\n\n\n<p>We are aiming to create a blog portal where customers can write blogs. <\/p>\n\n\n\n<p>So it makes much more sense that the page should appear like other customer account pages and instead of manually entering the URL we should have a menu in the customer navigation.<\/p>\n\n\n\n<p>To add the customer navigation menu we will follow the &#8220;<a href=\"https:\/\/webkul.com\/blog\/account-navigation-link-magento2\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento2 \u2013 Add New Link in My Account Navigation Panel<\/a>&#8221; blog. <\/p>\n\n\n\n<p>We have to create a customer_account.xml file under the view\/frontend\/layout folder. Magento already has this XML file we are just appending our code to it.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"429\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_102-1200x429.png\" alt=\"Selection_102\" class=\"wp-image-390604\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_102-1200x429.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_102-300x107.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_102-250x89.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_102-768x275.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_102.png 1515w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">Folder Structure<\/figcaption><\/figure>\n\n\n\n<p>Code for <em>view\/frontend\/layout\/customer_account.xml<\/em> file<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;page xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd&quot;&gt;\n    &lt;body&gt;\n        &lt;referenceBlock name=&quot;customer_account_navigation&quot;&gt;\n            &lt;block class=&quot;Magento\\Framework\\View\\Element\\Html\\Link\\Current&quot; name=&quot;customer-account-navigation-blog-add&quot; after=&quot;-&quot; &gt;\n                &lt;arguments&gt;\n                    &lt;argument name=&quot;label&quot; xsi:type=&quot;string&quot; translate=&quot;true&quot;&gt;Add Blog&lt;\/argument&gt;\n                    &lt;argument name=&quot;path&quot; xsi:type=&quot;string&quot;&gt;blog\/manage\/add&lt;\/argument&gt;\n                &lt;\/arguments&gt;\n            &lt;\/block&gt;\n        &lt;\/referenceBlock&gt;\n    &lt;\/body&gt;\n&lt;\/page&gt;<\/pre>\n\n\n\n<p>Now let&#8217;s modify our already created blogmanager_manage_add.xml file to match the customer account page structure.<\/p>\n\n\n\n<p>Updated code for <em>view\/frontend\/layout\/blogmanager_manage_add.xml<\/em> file<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;page xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd&quot;&gt;\n    &lt;update handle=&quot;customer_account&quot;\/&gt;\n    &lt;body&gt;\n        &lt;referenceContainer name=&quot;content&quot;&gt;\n            &lt;block class=&quot;Magento\\Framework\\View\\Element\\Template&quot; name=&quot;blogmanager.blog.add&quot; template=&quot;Webkul_BlogManager::add.phtml&quot; \/&gt;\n        &lt;\/referenceContainer&gt;\n    &lt;\/body&gt;\n&lt;\/page&gt;<\/pre>\n\n\n\n<p>Here you can see that we have removed the layout type and added the &lt;update handle=&#8221;customer_account&#8221;\/&gt; tag.<\/p>\n\n\n\n<p>Basically, it says that copies everything from the customer_account.xml, so it will copy all the structure from customer_account.xml which acts as a base for every customer account-related page.<\/p>\n\n\n\n<p>Now when we di compile and reload we will see something like,<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"620\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_23-52-1200x620.png\" alt=\"2021-02-06_23-52\" class=\"wp-image-282328\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_23-52-1200x620.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_23-52-300x155.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_23-52-250x129.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_23-52-768x397.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_23-52-1536x793.png 1536w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/02\/2021-02-06_23-52.png 1847w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><\/figure>\n\n\n\n<p>Folder structure till now,<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"429\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_103-1200x429.png\" alt=\"Selection_103\" class=\"wp-image-390607\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_103-1200x429.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_103-300x107.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_103-250x89.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_103-768x274.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_103.png 1515w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><figcaption class=\"wp-element-caption\">Folder Structure<\/figcaption><\/figure>\n\n\n\n<p>PS. Please run the di compile command if you create a new file, modify any .xml file, or make changes in the constructor of a class.<\/p>\n\n\n\n<p>Sometimes running di compile might not have any effect then we should explicitly remove the generated\/code folder by running rm -rf generated\/code from the Magento root directory. Please be careful while running the rm -rf command.<\/p>\n\n\n\n<p>Also you can take benefits of our <a href=\"https:\/\/webkul.com\/adobe-commerce-theme-template-development-services\/\">Magento 2 Template Development<\/a> service.<\/p>\n\n\n\n<p>Next Blog -&gt; <a href=\"https:\/\/webkul.com\/blog\/magento-development-06-form-validation-and-saving-data\/\">Magento 2 Development 09: Form Validation and Saving Data<\/a><\/p>\n\n\n\n<p>Previous Blog -&gt; <a href=\"https:\/\/webkul.com\/blog\/magento-development-04-model-resource-model-and-collection\/\">Magento 2 Development 07: Model, Resource Model and Collection<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now let&#8217;s come back to frontend development. Earlier we created a controller where I wrote a &#8220;Hello World&#8221; statement to show you that the controller is working. But in Magento, we don&#8217;t write HTML code in the controller. To show HTML content we use .phtml files in Magento. These .phtml files are called template files. <a href=\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":201,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121],"tags":[727,15564,256,138,2939],"class_list":["post-282315","post","type-post","status-publish","format-standard","hentry","category-magento-2","tag-blog","tag-github-login","tag-layout","tag-templates","tag-view"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Magento 2: Layout and Templates - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Magento 2: Layout and Templates - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Now let&#8217;s come back to frontend development. Earlier we created a controller where I wrote a &#8220;Hello World&#8221; statement to show you that the controller is working. But in Magento, we don&#8217;t write HTML code in the controller. To show HTML content we use .phtml files in Magento. These .phtml files are called template files. [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-06T18:29:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-21T08:05:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099-1200x331.png\" \/>\n<meta name=\"author\" content=\"Sanjay Chouhan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sanjay Chouhan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/\"},\"author\":{\"name\":\"Sanjay Chouhan\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/645580979f637b0e355deea21bd07462\"},\"headline\":\"Magento 2: Layout and Templates\",\"datePublished\":\"2021-02-06T18:29:13+00:00\",\"dateModified\":\"2025-01-21T08:05:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/\"},\"wordCount\":984,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099-1200x331.png\",\"keywords\":[\"Blog\",\"github login\",\"layout\",\"templates\",\"view\"],\"articleSection\":[\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/\",\"url\":\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/\",\"name\":\"Magento 2: Layout and Templates - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099-1200x331.png\",\"datePublished\":\"2021-02-06T18:29:13+00:00\",\"dateModified\":\"2025-01-21T08:05:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099.png\",\"width\":1517,\"height\":418,\"caption\":\"Selection_099\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Magento 2: Layout and Templates\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/645580979f637b0e355deea21bd07462\",\"name\":\"Sanjay Chouhan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cd6ee19f99bd1fcafef819135529c952d7c875d06fedd9fd4c4eb0996bafc1bd?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cd6ee19f99bd1fcafef819135529c952d7c875d06fedd9fd4c4eb0996bafc1bd?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Sanjay Chouhan\"},\"sameAs\":[\"https:\/\/www.instagram.com\/sanjaychouhansc\/\",\"https:\/\/in.linkedin.com\/in\/scchouhansanjay\"],\"url\":\"https:\/\/webkul.com\/blog\/author\/sanjay-chouhan180\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Magento 2: Layout and Templates - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/","og_locale":"en_US","og_type":"article","og_title":"Magento 2: Layout and Templates - Webkul Blog","og_description":"Now let&#8217;s come back to frontend development. Earlier we created a controller where I wrote a &#8220;Hello World&#8221; statement to show you that the controller is working. But in Magento, we don&#8217;t write HTML code in the controller. To show HTML content we use .phtml files in Magento. These .phtml files are called template files. [...]","og_url":"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2021-02-06T18:29:13+00:00","article_modified_time":"2025-01-21T08:05:51+00:00","og_image":[{"url":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099-1200x331.png","type":"","width":"","height":""}],"author":"Sanjay Chouhan","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Sanjay Chouhan","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/"},"author":{"name":"Sanjay Chouhan","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/645580979f637b0e355deea21bd07462"},"headline":"Magento 2: Layout and Templates","datePublished":"2021-02-06T18:29:13+00:00","dateModified":"2025-01-21T08:05:51+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/"},"wordCount":984,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099-1200x331.png","keywords":["Blog","github login","layout","templates","view"],"articleSection":["Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/","url":"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/","name":"Magento 2: Layout and Templates - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099-1200x331.png","datePublished":"2021-02-06T18:29:13+00:00","dateModified":"2025-01-21T08:05:51+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2023\/07\/Selection_099.png","width":1517,"height":418,"caption":"Selection_099"},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/magento-2-layout-and-templates\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Magento 2: Layout and Templates"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/645580979f637b0e355deea21bd07462","name":"Sanjay Chouhan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cd6ee19f99bd1fcafef819135529c952d7c875d06fedd9fd4c4eb0996bafc1bd?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cd6ee19f99bd1fcafef819135529c952d7c875d06fedd9fd4c4eb0996bafc1bd?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Sanjay Chouhan"},"sameAs":["https:\/\/www.instagram.com\/sanjaychouhansc\/","https:\/\/in.linkedin.com\/in\/scchouhansanjay"],"url":"https:\/\/webkul.com\/blog\/author\/sanjay-chouhan180\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/282315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/201"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=282315"}],"version-history":[{"count":20,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/282315\/revisions"}],"predecessor-version":[{"id":481150,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/282315\/revisions\/481150"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=282315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=282315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=282315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}