0

I want to add new phtml file to existed phtml file. It is working fine, but I want to know reference block and class names.

My code in layout file

    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>       
   <block class="Magento\Framework\View\Element\Template" template="Magento_Test::test.phtml"/>
</body>
In layout file, Here I need to add reference block/container or no need?

Existed code phtml file is

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Test::test.phtml")->toHtml();?>

1 Answer 1

1

Layout Method

If you want to added another phtml file using the layout, then you need to give name to your block like this one :

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>       
   <block class="Magento\Framework\View\Element\Template" name="mytestblock" as="mytestblock" template="Magento_Test::test.phtml"/>
</body>

then you can use like refrenceBlock="mytestblock" to call another phtml using layout.

Direct Method

You want to call phtml into another phtml then you can use this code.

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Test::newtest.phtml")->toHtml();?>
4
  • so existed block name i need to add in layout file like <referenceBlock name="existed block name"> <block class="Magento\Framework\View\Element\Template" template="Magento_Test::test.phtml"/> </referenceBlock>
    – User0434
    Nov 19, 2021 at 9:54
  • yes, you can use like that. Nov 19, 2021 at 10:00
  • If answer help, please upvote and accept as solution, so it will help other in community. Nov 19, 2021 at 10:16
  • sure, then i will give class name also existed name like this <block class="Magento\Test\Block\Testname" intstead of <block class="Magento\Framework\View\Element\Template"
    – User0434
    Nov 19, 2021 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.