0

I have a layered navigation module inside my Magento 2 custom created theme.

Now, I have to use this layered nav module's phtml file to another module just like catalog module's phtml file.

Can anyone please tell me how to do this?

Thanks in advanced.

2 Answers 2

0

you solution is here use this code in phtml file where u want to call layered navigation and set name of that block

getLayout()->getBlock('catalog.leftnav')->toHtml();?>
0
0

we can render block/template from other template files, but you need to know which block and template have the details after that you can render like below

<?php echo $this->getLayout()->createBlock("Vendor\Module\Block\YourBlock")->setTemplate("Vendor_Module::template.phtml")->toHtml(); ?> 

add the above code in whatever phtml file you need to display

for example I can render product detail page gallery

<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\View\Gallery")->setTemplate("Magento_Catalog::product/view/gallery.phtml")->toHtml(); ?>

Edit [not tested, I hope it works]

<?php echo $this->getLayout()->createBlock("Magento\LayeredNavigation\Block\Navigation")->setTemplate("Magento_LayeredNavigation::layer/view.phtml")->toHtml(); ?>
5
  • thanks for your reply, but if the phtml file which i have to call inside the phtml, is also connected to other two files. can we call all three files in the template. because whenever i am calling the phtml using above code, it results blank list in the frontend..
    – Yudi
    Sep 10, 2016 at 5:33
  • show your code and logs and also mention exactly what you want Sep 10, 2016 at 5:36
  • <?php echo $block->getLayout()->createBlock('LayeredNavigation\layer\view')->setTemplate('Magento_LayeredNavigation::layer/view.phtml')->toHtml(); ?> when i am calling like this. only view.phtml file is calling. but the view.phtml file also using two more phtml inside it just like(state.phtml & filter.phtml). so using this way only one file(view.phtml) we getting. but we want other two file also with this phtml file..
    – Yudi
    Sep 10, 2016 at 5:40
  • also when i see the xml file. <referenceContainer name="sidebar.main"> <block class="Magento\LayeredNavigation\Block\Navigation\Category" name="catalog.leftnav" before="-" template="layer/view.phtml"> <block class="Magento\LayeredNavigation\Block\Navigation\State" name="catalog.navigation.state" as="state" /> <block class="Magento\LayeredNavigation\Block\Navigation\FilterRenderer" name="catalog.navigation.renderer" as="renderer" template="layer/filter.phtml"/> </block> </referenceContainer>
    – Yudi
    Sep 10, 2016 at 5:44
  • your declaration was wrong see my updated answer. your xml file have 3 blocks so you need to call three different format to render each block. my answer contains render only one block. this is the key to render in other module phtml file you can extend yourself using this logic. Sep 10, 2016 at 5:51

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.