2013-08-04 29 views
1

我有2個模板, - /view/opinions/add.ctp - 添加表單 - /view/opinions/list.ctp - 顯示器意見CakePHP中合併視圖模板

,我希望他們在diplay/views/views/index.ctp有可能嗎?

是通過$ this - > element()完成它的唯一方法嗎?如果是這樣,我可以包含來自/ view/views而不是/ view/elements的模板嗎?

@edit

OpinionsController.php

class OpinionsController extends AppController { 
    public $helpers = array('Html', 'Form', 'Session'); 
    public $components = array('Session'); 

    var $name = 'Opinions'; 

    function index() { 
     $opinions = $this->Opinion->find('all'); 
     if(isset($this->params['requested'])) { 
      return $opinions; 
     } 
     $this->set('opinions', $opinions);  
    } 



    public function add() { 
     if ($this->request->is('post')) { 
      $this->Opinion->create(); 
      if ($this->Opinion->save($this->request->data)) { 
       $this->Session->setFlash(__('saved.')); 
       $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('Unable to add.')); 
      } 
     } 
    } 
} 

index.ctp

$this->extend('/Opinions/view'); 
$this->extend('/Opinions/add'); 

add.ctp

echo $this->Form->create('Opinion', array('type' => 'file')); 
echo $this->Form->input('author_name', array('label' => 'Imię')); 
echo $this->Form->input('author_signature', array('label' => 'Podpis')); 
echo $this->Form->input('text', array('rows' => '5', 'cols' => '30', 'label' => 'Opinia')); 
echo $this->Form->input('author_pic', array('type' => 'file', 'label' => 'Zdjęcie')); 
echo $this->Form->input('author_pic_dir', array('type' => 'hidden')); 
echo $this->Form->end('Dodaj opinię'); 

view.ctp `

<?php foreach ($opinions as $opinion): ?> 

    <div class="opinion"> 
     <div class="author"> 
      <div class="pic"><?php echo $this->Html->image("defaultAvatar.jpg", array('alt' => $opinion['Opinion']['author_name'])); ?></div> 
      <div class="signature"><b><?= $opinion['Opinion']['author_name']?></b><br><i><?= $opinion['Opinion']['author_signature']?></i></div> 
     </div> 
     <div class="text"> 
      <blockquote><p><?= $opinion['Opinion']['text']?></p></blockquote>  
     </div> 
     <div class="clear"><!-- . --></div> 
    </div> 

    <div class="clear"><!-- . --></div> 

<?php endforeach; ?> 
<?php unset($post); ?> 

`

+0

我們可以做到這一點,只需在index.ctp文件中添加兩個視圖代碼即可。添加你的邏輯添加和列出值..! –

回答

0

在像CakePHP的web框架,我們要保持我們的代碼幹。爲了做到你想做的事,我認爲更好的方法是創建元素來列出你的意見,另一種是用表單添加一個新的。

然後在index.ctp你會把這兩個元素。並且在add.ctp中,您只放置了具有表格的元素,並且在view.ctp中應該放入列出意見的元素。