2013-05-03 60 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') ?> 
相關問題