2

Help me to get the content written in custom phtml of the custom module to my custom theme.

I have custom theme Customtheme_Sample, which is having custom layout customhome.xml.

And I have the custom module Showcase_Newitems, here I have helloworld.phtml in templates. Now, I need to call helloworld.phtml from customhome.xml.

How Can I achieve this?

3
  • 1
    Can you update your codes? Aug 31 '18 at 12:16
  • How you are accessing the page to which this layout file customehome.xml belongs. Is this a controller or something else? Sep 26 '18 at 3:56
  • accept my answer if it solved your problem:) Apr 17 '19 at 4:30
8

try this in your layout file (customhome.xml)

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" layout="1column">
        <body>
            <referenceContainer name="content">
                <block class="Customtheme\Sample\Block\Helloworld" name="helloworld" template="helloworld.phtml" />
            </referenceContainer>
        </body>
    </page>
4

In Magento 2, you should not use your module's templates in the theme file anymore (like back in Magento 1). Instead, your module directory should contain the code and the view files.

In your module directory, add the directory view/frontend/layout. Then, add the correct layout file for the scope. You won't use customhome.xml. I assume you want to add something to the homepage, so simply create a file with the name cms_index_index.xml.

Then add the following code:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceContainer name="content">
                <block class="Myname\Mymodule\Block\Myblockname" name="customhome" template="Myname_Mymodule::customhome.phtml" />
            </referenceContainer>
        </body>
    </page>

The filename (cms_index_index.xml) determines where your content is shown (you don't use different layout handles inside the same XML file anymore). So, if you want to show it on every page, the file should be named default.xml; if you want to show it on product pages, the file should be named catalog_product_view.xml, etc.

0

app/design/frontend/Customtheme/sample/Magento_Theme/layout/default.xml

You call your phtml in your container like this:

<block class="Showcase\Newitems\Block\{BlockName}" name="helloworld.mod" template="Showcase_Newitems::helloworld.phtml" />
1
  • Why the downvote ? the one who did it, be brave and he explains to me why ?
    – Aamir
    Aug 31 '18 at 20:25

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.