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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With