6

I have a phtml file view.php and its block class View.php. I need to show that phtml on a modal on checkout page and to do that I'm pre-loading phtml file using following code :

<div id="modal-form" >
    <?php include ($block->getTemplateFile('XYZ_module::view.phtml')) ?>
</div>
<script>
    require(
        [
            'jquery',
            'Magento_Ui/js/modal/modal'
        ],
        function(
            $,
            modal
        ) {
            var options = {
                type: 'popup',
                responsive: true,
                innerScroll: true,
                title: 'Some Title',
                buttons: [{
                    text: $.mage.__('Continue'),
                    class: '',
                    click: function () {
                        this.closeModal();
                    }
                }]
            };

            var popup = modal(options, $('#modal-form'));
        }
    );
</script>

And then I'm opening this modal using following code :

$(".someBtn").click(function(event){
        event.preventDefault();
        $('#modal-form').modal('openModal');
});

Right now everything is working from loading in the phtml file, in the modal and opening that modal on a button click. What's not working is the functions of View.php block class. I think the issue is with the pre-loading in phtml file using include function and because of that the php functions are hitting the server once and not when I actually open the modal. Can I reload or just load that phtml file dynamically when the modal opens on a button click?

0

Best solution is to set a default block and add your template to it.

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

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.