{"id":322855,"date":"2022-02-19T05:49:25","date_gmt":"2022-02-19T05:49:25","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=322855"},"modified":"2024-07-26T08:04:50","modified_gmt":"2024-07-26T08:04:50","slug":"override-phtml-file-in-referenceblock-dynamically","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/","title":{"rendered":"Override phtml file in referenceBlock dynamically"},"content":{"rendered":"\n<p>Hello Friends!!!<\/p>\n\n\n\n<p>In this blog, we are going to learn how we can override a phtml file in referenceBlock dynamically in the layout file.<\/p>\n\n\n\n<p>For example, we have created a layout file <strong>custommodule_index_index.xml <\/strong>inside the <em>app\/code\/Vendor\/Custommodule\/view\/frontend\/layout\/ <\/em>directory. Now, we have to override the block &#8220;custom_block_home&#8221;.<\/p>\n\n\n\n<p>Then, we can override it by using the following methods:<\/p>\n\n\n\n<p><strong>Based on a check of the configuration field in the layout file:<\/strong> In this way, we add the &#8220;ifconfig&#8221; attribute in the &#8220;action&#8221; tag to set the template. Please look into the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;!--\n\/**\n * Vendor\n *\n * @category  Vendor\n * @package   Vendor_Custommodule\n * @author    Vendor\n * @copyright Vendor(https:\/\/example.com)\n * @license   https:\/\/example.com\/license.html\n *\/\n--&gt;\n&lt;page xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; layout=&quot;blank&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd&quot;&gt;\n    &lt;body&gt;\n        &lt;referenceBlock name=&quot;custom_block_home&quot;&gt;\n            &lt;action method=&quot;setTemplate&quot; ifconfig=&quot;custommodule\/configuration\/isactive&quot;&gt;\n                &lt;argument name=&quot;template&quot; xsi:type=&quot;string&quot;&gt;Vendor_Custommodule::home.phtml&lt;\/argument&gt;\n            &lt;\/action&gt;\n        &lt;\/referenceBlock&gt;\n    &lt;\/body&gt;\n&lt;\/page&gt;<\/pre>\n\n\n\n<p><strong>Based on a method&#8217;s result: <\/strong>In this way, we can call a method from a helper file, and according to the result of the method, the template will be set in block dynamically. Please refer to the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;!--\n\/**\n * Vendor\n *\n * @category  Vendor\n * @package   Vendor_Custommodule\n * @author    Vendor\n * @copyright Vendor (https:\/\/example.com)\n * @license   https:\/\/example.com\/license.html\n *\/\n--&gt;\n&lt;page xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; layout=&quot;blank&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd&quot;&gt;\n    &lt;body&gt;\n        &lt;referenceBlock name=&quot;custom_block_home&quot;&gt;\n            &lt;action method=&#039;setTemplate&#039;&gt;\n                &lt;argument name=&quot;template&quot; xsi:type=&quot;helper&quot; helper=&quot;Vendor\\Custommodule\\Helper\\Data::getHomeTemplate&quot;&gt;\n                &lt;\/argument&gt;\n            &lt;\/action&gt;\n        &lt;\/referenceBlock&gt;\n    &lt;\/body&gt;\n&lt;\/page&gt;<\/pre>\n\n\n\n<p>We have created the following method, in the <em>Data.php<\/em> file inside the <em>app\/code\/Vendor\/CustomModule\/Helper\/ <\/em>directory.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">    \/**\n     * Get Home Template\n     *\n     * @return string\n     *\/\n    public function getHomeTemplate()\n    {\n        $template = &quot;&quot;;\n        $check = $this-&gt;isModEnabled(); \/\/you can call here your method to get the result for required condition\n        if ($check) {\n            $template =  &#039;Vendor_Custommodule::home.phtml&#039;;\n        } else {\n            $template = &#039;Vendor_PrevCustommodule::home.phtml&#039;;\n        }\n\n        return $template;\n    }<\/pre>\n\n\n\n<p>Hope this will be helpful. Thanks \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Friends!!! In this blog, we are going to learn how we can override a phtml file in referenceBlock dynamically in the layout file. For example, we have created a layout file custommodule_index_index.xml inside the app\/code\/Vendor\/Custommodule\/view\/frontend\/layout\/ directory. Now, we have to override the block &#8220;custom_block_home&#8221;. Then, we can override it by using the following methods: <a href=\"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":249,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121],"tags":[12418,12419,12420,12417,12416],"class_list":["post-322855","post","type-post","status-publish","format-standard","hentry","category-magento-2","tag-override-phtml","tag-override-phtml-file-dynamically","tag-override-phtml-file-in-referenceblock-dynamically","tag-reference-block","tag-referenceblock"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Override phtml file in referenceBlock dynamically - Webkul Blog<\/title>\n<meta name=\"description\" content=\"Override phtml file in referenceBlock dynamically, how we can override a phtml file in referenceBlock dynamically in the layout file.\" \/>\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\/override-phtml-file-in-referenceblock-dynamically\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Override phtml file in referenceBlock dynamically - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Override phtml file in referenceBlock dynamically, how we can override a phtml file in referenceBlock dynamically in the layout file.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/\" \/>\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=\"2022-02-19T05:49:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-26T08:04:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Khushboo Sahu\" \/>\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=\"Khushboo Sahu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/\"},\"author\":{\"name\":\"Khushboo Sahu\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\"},\"headline\":\"Override phtml file in referenceBlock dynamically\",\"datePublished\":\"2022-02-19T05:49:25+00:00\",\"dateModified\":\"2024-07-26T08:04:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/\"},\"wordCount\":170,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"override phtml\",\"override phtml file dynamically\",\"Override phtml file in referenceBlock dynamically\",\"reference block\",\"referenceBlock\"],\"articleSection\":[\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/\",\"url\":\"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/\",\"name\":\"Override phtml file in referenceBlock dynamically - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2022-02-19T05:49:25+00:00\",\"dateModified\":\"2024-07-26T08:04:50+00:00\",\"description\":\"Override phtml file in referenceBlock dynamically, how we can override a phtml file in referenceBlock dynamically in the layout file.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Override phtml file in referenceBlock dynamically\"}]},{\"@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\/f94b8f53397bf85810761d76c98fadca\",\"name\":\"Khushboo Sahu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"caption\":\"Khushboo Sahu\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Override phtml file in referenceBlock dynamically - Webkul Blog","description":"Override phtml file in referenceBlock dynamically, how we can override a phtml file in referenceBlock dynamically in the layout file.","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\/override-phtml-file-in-referenceblock-dynamically\/","og_locale":"en_US","og_type":"article","og_title":"Override phtml file in referenceBlock dynamically - Webkul Blog","og_description":"Override phtml file in referenceBlock dynamically, how we can override a phtml file in referenceBlock dynamically in the layout file.","og_url":"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2022-02-19T05:49:25+00:00","article_modified_time":"2024-07-26T08:04:50+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Khushboo Sahu","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Khushboo Sahu","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/"},"author":{"name":"Khushboo Sahu","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca"},"headline":"Override phtml file in referenceBlock dynamically","datePublished":"2022-02-19T05:49:25+00:00","dateModified":"2024-07-26T08:04:50+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/"},"wordCount":170,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["override phtml","override phtml file dynamically","Override phtml file in referenceBlock dynamically","reference block","referenceBlock"],"articleSection":["Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/","url":"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/","name":"Override phtml file in referenceBlock dynamically - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2022-02-19T05:49:25+00:00","dateModified":"2024-07-26T08:04:50+00:00","description":"Override phtml file in referenceBlock dynamically, how we can override a phtml file in referenceBlock dynamically in the layout file.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/override-phtml-file-in-referenceblock-dynamically\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Override phtml file in referenceBlock dynamically"}]},{"@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\/f94b8f53397bf85810761d76c98fadca","name":"Khushboo Sahu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","caption":"Khushboo Sahu"},"url":"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/322855","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\/249"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=322855"}],"version-history":[{"count":4,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/322855\/revisions"}],"predecessor-version":[{"id":454897,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/322855\/revisions\/454897"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=322855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=322855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=322855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}