I am trying to get the same block on two custom phtml files,

  1. abc.phtml here i need selected data from array,

  2. xyz.phtml here i need all data from array, how to achieve this?

My format:

 <?php echo $this->getLayout()->createBlock("Amasty\Storelocator\Block\Location")->setTemplate("Amasty_Storelocator::center.phtml")->toHtml(); ?>
  • Update your question with proper eg. what you want to achieve Mar 15, 2021 at 12:52
  • @RakeshVarma I am using a third-party extension, i need the block into my two separate custom phtml file, any ref thanks.
    – zus
    Mar 15, 2021 at 12:56
  • if you want to use that block in your phtml file then you can add that block in layout file where you define your template file. Mar 15, 2021 at 12:59
  • My template under : app\design\frontend\Zero\my_theme\Magento_Theme\templates\location_link.phtml, how to include template file layout xml file?
    – zus
    Mar 15, 2021 at 13:01
  • how loation_link.phtml will render? or other word on which page or section it will render? Mar 15, 2021 at 13:03

3 Answers 3


You can call phtml inside phtml like this.

<referenceBlock name="header.panel">
    <block class="Magento\Framework\View\Element\Template" name="mycustom.block" template="Magento_Theme::location_link.phtml" after="-" >
        <block class="Amasty\Storelocator\Block\Location" name="custom.location.block" template="Amasty_Storelocator::center.phtml"/>

and inside location_link.phtml file add below code

<?= $this->getChildHtml('custom.location.block');?>

as discussed in chat this code is worked for you.


You can call in both phtml files same block

For ex your block class is

namespace Vandor\Module\Block;
use Magento\Framework\View\Element\Template;

class Main extends Template
    public function getMyCustomMethod()
        return '<p>CustomMethod</p>';

in your phtml file

$blockObj= $block->getLayout()->createBlock('Vandor\Module\Block\Main');
echo $blockObj->getMyCustomMethod();

upvote if helps

  • Blocks should be created via layouts, not in templates...
    – paj
    Mar 15, 2021 at 16:50
  • you can achieve everything by this method also
    – Asad Ullah
    Mar 16, 2021 at 6:31

You can pass data to block

 <?php echo $this->getLayout()->createBlock("Amasty\Storelocator\Block\Location")->setCustomData($array)->setTemplate("Amasty_Storelocator::center.phtml")->toHtml(); ?>

and get in phtml

<?php $retriveArray = $block->getCustomData(); ?>

Your Answer

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

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