0

I want to call show.phtml file which will print database records from entry.phtml file.Here is the code of my files having-

show.phtml

<?php
 $model=Mage::getModel('entry/entry')->getCollection()->getData();
 echo "<pre>";
 print_r($model);
 exit();

 ?>

entry.phtml

<h1>HELLO</h1>
<form enctype="multipart/form-data" method="post" action="<?php echo $this->getBaseUrl().'entry/index/save' ?>">
    <input type="text" name="name" placeholder="Enter Name">
    <input type="text" name="email" placeholder="Enter Email">
    <input type="text" name="mobile" placeholder="Enter Mobile">
    <input type="submit" value="Enter">

</form>

3 Answers 3

3

You can call your phtml file in another phtml file as below

<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('module-folder-name/show.phtml')->toHtml() ?> 

But if you want to show your phtml file from controller as you commented below my answer, you can call it as below.

Create a phtml file

app/design/frontend/your/theme/template/module/show.phtml

<?php
 $model=Mage::getModel('entry/entry')->getCollection()->getData();
 echo "<pre>";
 print_r($model);
 exit();

 ?>

Create an xml file

app/design/frontend/your/theme/layout/module.xml

<?xml version="1.0"?>   
<layout version="0.1.0">   
  <module_index_index>   
    <reference name="root">   
      <action method="setTemplate"><template>page/1column.phtml</template></action>   
    </reference>   
    <reference name="content">   
      <block type="module/index" name="module_index" template="module/show.phtml"/>   
    </reference>   
  </module_index_index>   
</layout>   

Create a controller file

app/code/local/Vendor/Module/controllers/IndexController.php

<?php
class Vendor_Module_IndexController extends Mage_Core_Controller_Front_Action{
    public function IndexAction() {

    $this->loadLayout();   
    $this->renderLayout(); 

    }
}
4
  • it works ,but open show.phtml file on same page. how to open show.phtml file in new tab ?
    – Akshay10
    Commented Oct 9, 2017 at 6:17
  • so what do you want exactly ? Commented Oct 9, 2017 at 6:22
  • to create a new function (showAction) in indexController and open show.phtml file, using controller function .
    – Akshay10
    Commented Oct 9, 2017 at 6:25
  • See my updated code. I have used IndexAction. You can use it as you want. Commented Oct 9, 2017 at 6:40
1

Place the bellow code where you want to call your file:

<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('path/to/show.phtml')->toHtml() ?> 
1
  • Actually he needs to show his phtml file by using controller action. Commented Oct 9, 2017 at 8:01
0

To call .phtml file through controller:

try this code--

public function showAction()
    {


    $block = $this->getLayout()->createBlock('core/template');

    // Assign your template to it
    // This path is relative to your current theme (eg: rwd/default/template/...)
    $block->setTemplate('entry/show.phtml');

    // Render the template to the browser
    echo $block->toHtml();
  }

}

in your main template(.phtml file) write this code:

<a href="<?php echo $this->geturl("entry/index/show"); ?>">Show</a>

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.