cancel
Showing results for 
Search instead for 
Did you mean: 

How to call phtml file in another phtml file

SOLVED

How to call phtml file in another phtml file

hi guys,

              Here i have two phtml files i.e

home.phtml:

path:  /var/www/html/gr/app/code/Magestore/Webpos/view/frontend/templates/home.phtml

code :

<script type="text/javascript">
var nwIp4= "192.168.0.4";

window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new RTCPeerConnection({iceServers:[]}), noop = function(){};
pc.createDataChannel("");
pc.createOffer(pc.setLocalDescription.bind(pc), noop);
pc.onicecandidate = function(event)
{
var s = event.candidate.candidate;
var result = s.split(" ");
var nwIp1 = result[4];
var mIp= 'nwip='+nwIp1;
if(nwIp1 == nwIp4)
{
       //here i want to call login.phtml in magento 2

}
}
</script>

login.phtml:

path : /var/www/html/gr/app/code/Magestore/Webpos/view/frontend/templates/login.phtml

 

and now i want to call login.phtml file in home,phtml within script tag.

can any one help me on that ?

 

2 ACCEPTED SOLUTIONS

Accepted Solutions

Re: How to call phtml file in another phtml file

Hi @bharath553

 

Use this code to call phtml file in script

 

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


If you have custom block for phtml then use block

<?php echo $this->getLayout()->createBlock("Magestore\Webpos\Block\MyBlock")->setTemplate("Magestore_Webpos::login.phtml")->toHtml() ?>

So you final code will look like this

 

if(nwIp1 == nwIp4)
{
    <?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magestore_Webpos::login.phtml")->toHtml() ?>
}

 

If Issue Solved, Click Kudos/Accept As solutions.

View solution in original post

Re: How to call phtml file in another phtml file

you can use following code

<?php
echo $this->getLayout()->createBlock("QaisarSatti\HelloWorld\Block\HelloWorld")->setTemplate("QaisarSatti_HelloWorld::helloworld.phtml")->toHtml();
?>



Reference

Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"
Qaisar Satti

View solution in original post

5 REPLIES 5

Re: How to call phtml file in another phtml file

Hi @bharath553

 

Use this code to call phtml file in script

 

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


If you have custom block for phtml then use block

<?php echo $this->getLayout()->createBlock("Magestore\Webpos\Block\MyBlock")->setTemplate("Magestore_Webpos::login.phtml")->toHtml() ?>

So you final code will look like this

 

if(nwIp1 == nwIp4)
{
    <?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magestore_Webpos::login.phtml")->toHtml() ?>
}

 

If Issue Solved, Click Kudos/Accept As solutions.

Re: How to call phtml file in another phtml file

code:

----------

<script type="text/javascript">
var nwIp4= "192.168.0.4";

window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new RTCPeerConnection({iceServers:[]}), noop = function(){};
pc.createDataChannel("");
pc.createOffer(pc.setLocalDescription.bind(pc), noop);
pc.onicecandidate = function(event)
{
var s = event.candidate.candidate;
var result = s.split(" ");
var nwIp1 = result[4];

if(nwIp1 == nwIp4)
{
<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magestore_Webpos::login.phtml")->toHtml(); ?>
}
}</script>

after implementing the above code  result is :

-------------------------------------------------------------Screenshot (37).png

 

 

Re: How to call phtml file in another phtml file

Hi Mr patel your code is working fine but with in script tag its not working.Out of script tag its working good. can you help me on that ?

Re: How to call phtml file in another phtml file

you can use following code

<?php
echo $this->getLayout()->createBlock("QaisarSatti\HelloWorld\Block\HelloWorld")->setTemplate("QaisarSatti_HelloWorld::helloworld.phtml")->toHtml();
?>



Reference

Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"
Qaisar Satti

Re: How to call phtml file in another phtml file

<?php
echo $this->getLayout()->createBlock("QaisarSatti\HelloWorld\Block\HelloWorld")->setTemplate("QaisarSatti_HelloWorld::helloworld.phtml")->toHtml();
?>

the above calling method is right but its not working inside script tag 

code:

--------

<script type="text/javascript">
var nwIp4= "192.168.0.4";

window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new RTCPeerConnection({iceServers:[]}), noop = function(){};
pc.createDataChannel("");
pc.createOffer(pc.setLocalDescription.bind(pc), noop);
pc.onicecandidate = function(event)
{
var s = event.candidate.candidate;
var result = s.split(" ");
var nwIp1 = result[4];

if(nwIp1 == nwIp4)
{
<?php
echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magestore_Webpos::login.phtml")->toHtml();
?>
}
}

</script>