Template and View best practices

Hi Everyone,

I have a couple of pages where I allow users to soft delete and restore records (the SoftDeleteTrait works very well here).

I also allow users to [show | hide] deleted records. There are two separate actions that drive which to show, /index and /all. At first I also had two separate templates, index.ctp and all.ctp.

In an effort to consolidate, I got rid of all.ctp and added some if/else clauses to index.ctp to toggle whether to display the “Show Deleted” and “Hide Deleted” links. I also modified the /all action in the controller to render index.ctp.

This is where my question comes in: is what I did bad practice? I feel clauses like

<?php if ($this->request->getParam('action') == 'all'): ?>

in a template file is dirty. What’s the consensus on this?

I think that the difference should be in the controller instead of the views. Something like this

public function index()
{
    // ...
    $query = ..... ->find('index');
    $page = $this->paginate($query);
    // ....
}


public function all()
{
    // ...
    $query = ..... ->find('index')->find('all');
    $page = $this->paginate($query);
    $this->viewBuilder()->template('index');
    // ...
}

If you are using crud, you could setUp an Index action with a custom finder in the controller beforeFilter

Raul, I already have the finder logic defined properly in my controller.

The question centers on the presentation of a link that either displays “Show Deleted” or “Hide Deleted.” I have logic in my template that looks like this:

<?php if ($this->request->getParam('action') == 'all'): ?>
    <li><?= $this->Html->link(__('Hide Deleted'), ['action' => 'index']) ?></li>
<?php else: ?>
    <li><?= $this->Html->link(__('Show Deleted'), ['action' => 'all']) ?></li>
<?php endif; ?>

Is this OK or bad practice?

Sure, I use that approach for “index” and “closed” (history), I don’t know if there are other ways