1
votes

I have 2 templates, - /view/opinions/add.ctp - add form - /view/opinions/list.ctp - displays opinions

and I want them diplay in /views/opinions/index.ctp is it possible?

Is the only way to do it by $this -> element() ? if so, can I include templates from /view/opinions instead of /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); ?>

`

1
we can do it, just add ur both view code in index.ctp file.. and in index action of ur controller. add ur logic for add and list the values..!!jsduniya

1 Answers

0
votes

In web frameworks like CakePHP, we want to keep our code DRY. In order to do what you want, I think the better approach is to create elements to list your opinions and another with the form to add a new one.

Then in index.ctp you would put these two elements. And in add.ctp you put only the element with the form and in view.ctp you should put the element that list the opinions.