2013-05-03 117 views
5

我只是新的CakePhp,我想知道如何在另一个视图中调用视图。如何通过使用cakephp调用另一个视图中的视图

当我开始运行CakePhp时,默认布局位于view/layouts/default.ctp

default.thtml中我称为视图名称homeview(视图/家庭/ homeview.ctp)。

这里是我的代码:

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

而在homeview.ctp我叫另一个名为displayphone视图(视图/家庭/ displayphone.ctphomeview.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(); ?> 

为什么我不能把在homeview displayphone块?

回答

8

至于你提到的

$this->fetch('homeview'); 

已通过名称homeview创建块,请参阅本http://book.cakephp.org/2.0/en/views.html

至于调用视图中另一种观点认为,除非你创建一个元素是不可能的。元素是可以在任何视图文件中使用的通用HTML集。对于上述目的在里面视图元素文件夹名“displayphone.ctp”创建一个元素,然后调用它像

$this->element('displayphone'); 

希望这将解决你的目的。

+0

感谢现在运转。 – 2013-05-03 08:55:37

+0

如何包含'ctp'? 'homeview'和'displayphone'都是同一个目录。我不需要做任何改变。我不想创建一个'元素' – Chinmay235 2014-09-10 04:40:33

0

为此,您需要创建可包含在任何视图文件中的元素 。

2

是的,你可以调用另一个视图内的视图(不好,但你可以做到这一点)。

例如,你有鉴于2次 查看/用户/ login.ctp 查看/用户/ register.ctp

,并要拨打注册登录查看视图中。

// File login.ctp 
$this->Element('../Users/register.ctp'); // ('..' . DS . 'Users' . DS . 'register.ctp') 

这将获得注册视图作为一个元素,并正常工作。

0

对于CakePHP的3,使用此

<?= $this->requestAction('/Users/register') ?> 
相关问题