Mab KaaKoo Mab KaaKoo - 6 months ago 11
PHP Question

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:

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

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

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


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

Why I can not call displayphone block in homeview ?


As you mentioned,


has created a block by name homeview, Refer this

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 through out the project. for above purpose create an element by name "displayphone.ctp" in Element folder inside view and then call it like


Hope this will solve your purpose.