在cakephp中合并视图模板

在cakephp中合并视图模板,php,templates,cakephp,view,Php,Templates,Cakephp,View,我有两个模板, -/view/OPTIONS/add.ctp-添加表单 -/view/opinions/list.ctp-显示意见 我想让它们显示在/views/opinions/index.ctp中。有可能吗 是通过$this->element()实现的唯一方法吗?如果是,我是否可以包含来自/view/opinions而不是/view/elements的模板 @编辑 opinioncontroller.php class OpinionsController extends AppContro

我有两个模板, -/view/OPTIONS/add.ctp-添加表单 -/view/opinions/list.ctp-显示意见

我想让它们显示在/views/opinions/index.ctp中。有可能吗

是通过$this->element()实现的唯一方法吗?如果是,我是否可以包含来自/view/opinions而不是/view/elements的模板

@编辑

opinioncontroller.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 `




`在CakePHP这样的web框架中,我们希望保持代码干燥。为了做你想做的事情,我认为更好的方法是创建元素来列出你的观点,并使用表单添加一个新的元素


然后在
index.ctp
中放置这两个元素。在
add.ctp
中,您只需将元素与表单一起放入
view.ctp
中,您应该将列出意见的元素放入其中。

我们可以这样做,只需将您的两个视图代码都添加到index.ctp文件中即可。。在ur控制器的指示作用下。为添加添加添加逻辑并列出值。。!!
<?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); ?>