7

I am trying to load a phtml file like this:

File /app/design/frontend/name/themename/Magento_Theme/layout/default.xml

    <referenceContainer name="page.top">
        <block class="Magento\Framework\View\Element\Template" name="startphoto" template="html/startphotos.phtml">
            <arguments>
                <argument name="section" xsi:type="string">homepage</argument>
                <argument name="position" xsi:type="number">0</argument>
            </arguments>
        </block>
    </referenceContainer>

I want to load it only on homepage, but it is still loading on every page. The arguments seems to be ignored.

20

As per as, Magento2, default.xml is call at every page.If you add this code at default.xml then it would be automatically call every pages of your current theme.

If you know about Magento 1.x handler concept then you can understand that default is a handle which is called at every pages.In Magento 2.X,every handler is individual layout files.

So if want to add a phtml file only at home page then you need to add that code at cms_index_index.xml (/app/design/frontend/name/themename/Magento_Cms/layout/cms_index_index.xml) layout file because of cms_index_index is handler which call only called at home

  • If I need to call my customise module in product page using only module XML file, how to do it ? Like add download linked in before add to cart button without change on catalog_product_view.xml file. – Hitesh Vaghasiya Aug 21 '17 at 4:56
  • If you created a custom module then create catalog_product_view.xml at your app\code\[vendorname]\[ModuleName]view\frontend\layout* write at here – Amit Bera Aug 21 '17 at 5:03
8

You can use xml to do this task
Inside cms_index_index.xml put xml like this

<referenceContainer name="content">
        <container name="block.container" htmlTag="div" htmlId="slider.container" htmlClass="block-home-container" before="">
            <block class="Magento\Framework\View\Element\Template" name="block.banner" as="block.custom.cms" template="Magento_Theme::block.phtml" after="-" />
        </container>
</referenceContainer>
5

Create xml file name with /app/design/frontend/Vendor/theme/Magento_Theme/layout/cms_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <referenceContainer name="page.top">
     <block class="Magento\Framework\View\Element\Template" name="startphoto" template="Magento_Theme::html/startphotos.phtml">
        <arguments>
            <argument name="section" xsi:type="string">homepage</argument>
            <argument name="position" xsi:type="number">0</argument>
        </arguments>
     </block>
  </referenceContainer>         
</page>
  • 2
    better include the path for the xml file,it will improve the answer more.Thanks – Mujahidh Mar 29 '16 at 7:41
  • If you use this XML file like this, where would the html/startphotos.phtml be, relative to the xml file? I can't figure it out. – Alex Timmer Apr 13 '16 at 15:08
  • 1
    updated answer check it – Prashant Valanda Apr 13 '16 at 16:30

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.