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
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
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
Try below steps
Step:1 Create default.xml
at 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
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>
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>