Skip to content
Advertisement

How to call view in another view by using cakephp

I am just new CakePhp, i wonder how to call a view in another view.

When I started to run CakePhp the default layout is located in view/layouts/default.ctp.

In default.ctp I called a view name homeview (view/homes/homeview.ctp).

Here is my code:

<?php 
     echo $this->fetch('homeview'); // this statement here is work
?>

And in the homeview.ctp I called another view named displayphone (view/homes/displayphone.ctp) homeview.ctp

<?php $this->start('homeview'); ?>
    <h1> This is home view </h1>
    <?php echo $this->fetch('displayphone'); // this statement does not work; ?>
<?php $this->end(); ?>

displayphone.ctp

<?php $this->start('displayphone');?>
    <h1> This page display phone </h1>
<?php $this->end(); ?>

Why I can not call displayphone block in homeview ?

Advertisement

Answer

As you mentioned,

 $this->fetch('homeview');

has created a block by name homeview, Refer this http://book.cakephp.org/2.0/en/views.html

As far as calling another view inside the view is not possible unless you create an element for that. Element are the common set of HTML which can be used in any view file throughout the project. For above purpose create an element by name “displayphone.ctp” in Element folder inside view and then call it like

 $this->element('displayphone');

Hope this will solve your purpose.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement