How to load a .phtml page content in a popup in magento 2?
-
09-10-2020 - |
Question
I need to load a .phtml content on a popup.
<div>
<a href="#" id="click-me">Click Me</a>
</div>
<div id="popup-mpdal" style="display:none;">
<h1> Hi I'm here.... </h1>
<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Namespace_Customtab::new.phtml")->toHtml();?>
</div>
<script>
require(
[
'jquery',
'Magento_Ui/js/modal/modal'
],
function(
$,
modal
) {
var options = {
type: 'popup',
responsive: true,
innerScroll: true,
title: 'popup mpdal title',
buttons: [{
text: $.mage.__('Continue'),
class: '',
click: function () {
this.closeModal();
}
}]
};
var popup = modal(options, $('#popup-mpdal'));
$("#click-me").on('click',function(){
$("#popup-mpdal").modal("openModal");
});
}
);
</script>
This is my code. How to load another phtml content instead of that div ? Please help me In find a solution.
custom file path: app/code/Namespace/Customtab/view/frontend/templates/product/view/new.phtml
Solution 2
Got the answer to load phtml file:
<div id="popup-mpdal" style="display:none;">
<?php include ($block->getTemplateFile('Namespace_Customtab::image.phtml')) ?>
</div>
By this, we can popup the custom phtml page.
OTHER TIPS
You can try with below code,
if your phtml file contains any Custom Block then replace Magento\Framework\View\Element\Template
with your custom block path
.
<div id="popup-mpdal" style="display:none;">
<h1> Hi I'm here.... </h1>
<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Namespace_Customtab::product/view/new.phtml")->toHtml();?>
</div>
try this.
`<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Namespace_Module::name_of_phtml.phtml")->toHtml(); ?>`
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange