Magento 2
Add a Template Block to a Page
Free Preview
app/code/Foo/Bar/view/frontend/templates/baz.phtml
<!-- A standard PHTML template that contains the info you wish to display. -->
<p><?= __('This is a description') ?></p>
app/code/Foo/Bar/view/frontend/layout/cms_index_index.xml
<?xml version="1.0"?>
<!--
The file cms_index_index.xml targets {routeId}_{controllerName}_{actionName}.
In this case, the routeId is "cms" and will target the index controller and
action. This will make this layout XML file only apply to the home page.
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <!--
        Create a "referenceContainer" node, using the name of the container or
        block you wish to place your template within.
        -->
        <referenceContainer name="content">
            <!--
            Create a new "block" child node. The "name" is any arbitrary name,
            usually delineated with periods or underscores. The "template"
            attribute expects the string syntax of a module, in the format
            "VendorName_ModuleName::template-name.phtml".
            
            The location after the double colons resolves to
            view/frontend/templates of the main module directory. For example,
            the following code resolves to look for a file at the location:
            app/code/Foo/Bar/view/frontend/templates/baz.phtml
            -->
            <block name="foo.bar" template="Foo_Bar::baz.phtml"/>
        </referenceContainer>
    </body>
</page>
Want to learn more?

Visit M.academy to learn much more about Magento, Laravel, PHP, Javascript, & Docker.

M.academy logo