1

I am using magento 2.4.1 I have created a basic module, I am trying to call a phtml file in all pages. I am using default.xml file (Separate for each scope) & calling a phtml file.

But there is some issue in default.xml file.

Can you please post proper code for default.xml for calling phtml in it ?

Thankyou

1
  • you can create seprate phtml for frontend and backend Jan 28, 2021 at 7:37

1 Answer 1

0

You could try doing something like this in

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

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="main.content">
        <block name="my.block" template="<Vendor>_<Module>::myblock.phtml">
            <arguments>
                <argument name="view_model" xsi:type="object"><Vendor>\<Module>\ViewModel\MyBlock</argument>
            </arguments>
        </block>
    </referenceContainer>
</body>

But this method implies that you use a view model instead of a block class, which is a better way of rendering something on the frontend. Also, on line 4 you need to reference a container or block where you want to put your template. You can see a full list of containers and blocks in vendor/magento/module-theme/view/frontend/layout/default.xml

3
  • do i need to make default.xml for adminhtml too ?
    – dev php
    Jan 28, 2021 at 7:17
  • I guess i can use same view model as common view model for admin & frontend. is that true ?
    – dev php
    Jan 28, 2021 at 7:17
  • Can you maybe be more specific where in the admin app you want your template to be rendered? I am asking because the answer to your question depends on this. Please elaborate what your module is supposed to do, maybe with an example, and I will be more able to help you. Jan 28, 2021 at 10:49

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.