4

I want to add my custom.phtml file on the top of all pages.

How can I do this using custom module ?

Any suggestion would be appreciated.

Thanks

3

4 Answers 4

17

Add this code in your custom theme or module in default.xml:

app/design/frontend/Namespace/Themename/Magento_Theme/layout/default.xml

<referenceContainer name="after.body.start">
    <block class="Magento\Framework\View\Element\Template" 
           name="custom.file" 
           before="-" 
           template="Magento_Theme::html/custom.phtml"
    />
</referenceContainer>

you can also place it in your module:

app/code/Vendor/Module/view/frontend/layout/default.xml

<referenceContainer name="after.body.start">
    <block class="Vendor\Module\Block\Blockname" 
           before="-" 
           template="Vendor_Module::html/custom.phtml"
    />
</referenceContainer>

In this case the template must be saved under app/code/Vendor/Module/view/frontend/templates/html/custom.phtml

2
  • @D Singh, I need to achieve this under app/code. Could you please answer my question in this regard at magento.stackexchange.com/questions/312363/… May 11, 2020 at 19:04
  • @CodeForGood Just add your module name like Vendor_Module instead Magento_Theme. I have provided the answer for both app/code and app/design.
    – D Singh
    May 12, 2020 at 6:14
7

Try below steps

Step:1 Create default.xmlat location app\code\<Vendor-Name>\<Module-Name>\view\frontend\layout and add below code

<referenceContainer name="page.top">
    <block class="Magento\Framework\View\Element\Template" name="custom-block-name" template="<Vendor-Name>_<Module-Name>::page\custom.phtml"/>
</referenceContainer>

OR

 <referenceContainer name="main.content">
    <block class="Magento\Framework\View\Element\Template" name="custom-block-name" template="<Vendor-Name>_<Module-Name>::page\custom.phtml"/>
</referenceContainer>

Step:2 Create custom.phtml at location app\code\<Vendor-Name>\<Module-Name>\view\frontend\templates\page\ and add your code

for eg.

<?php 
     echo "Custom Block";
?>

Step:3 After run compile command

php bin/magento setup:di:compile
1
  • your answer worked for me. I would like to know I can replace <referenceContainer name="page.top"> with <referenceBlock name="head.additional">. Please let me know. May 11, 2020 at 20:29
3

Add Custom Template file using xml block into app/design/frontend/<VendorName>/<ThemeName>/Magneto_Theme/layout/default.xml

Using referenceBlock tag you can add your custom block into header block/container

<referenceContainer name="after.body.start">
        <block class="Magento\Framework\View\Element\Template" name="custom.content" before="page-header" template="Magento_Theme::custom.phtml" />
</referenceContainer>
2
  • I would like to know I can replace <referenceContainer name="after.body.start"> with <referenceBlock name="head.additional">. Please let me know. May 11, 2020 at 20:30
  • add reference container of block "head.additional" and then you can replace your referenceBlock May 12, 2020 at 11:35
1

YOu can use page.wrapper to show before header tag.

<referenceContainer name="page.wrapper">
      <block class="Magento\Framework\View\Element\Template" name="custompage" before="-" template="{Namespace}_{Modulename}::custompage.phtml" />
 </referenceContainer>
1
  • I would like to know I can replace <referenceContainer name="page.wrapper"> with <referenceBlock name="head.additional">. Please let me know. May 11, 2020 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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