cancel
Showing results for 
Search instead for 
Did you mean: 

Add custom template in footer container

Add custom template in footer container

Hi,

In magento 2, I want to include a custom template in before body end container. Below file and function details which i used to load the template and I am not using controller in my module but in website the template not loading. Please correct the below code and help me to solve the problem.

app/code/Namespace/ModuleName/view/frontend/layout/tracking.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">
        <body>
         <referenceContainer name="before.body.end">
            <block class="Namespace\ModuleName\Block\Tracking" name="tracking" as="tracking" template="Namespace_ModuleName::tracking.phtml" />
        </referenceContainer>
        </body>
</page>
app/code/Namespace/ModuleName/view/frontend/templates/tracking.phtml
<div id="targetbay_message"></div>

<?php echo $block->sayHello(); ?>

app/code/Namespace/ModuleName/Block/Tracking.php
<?php
namespace Namespace\ModuleName\Block;

class Tracking extends \Magento\Framework\View\Element\Template {
    
    public function __construct(\Magento\Framework\View\Element\Template\Context $context)
    {
        parent::__construct($context);
    }

    public function sayHello()
    {
        return __('Hello World');
    }
}

 

Thanks,

Arun.

1 REPLY 1

Re: Add custom template in footer container

Hello arunvijay,

 

The problem is with your layout file name. Magento 2 follows the layout name as frontname_controller_action. Since you don't have any controller your layout file has not called at all.

 

If you want to display it in any specific page like product detail page then the layout name should be catalog_product_view.xml or if you want display your block in all the pages rename it to default.xml

 

Hope it helps Smiley Happy