Dharmesh patel
Dharmesh patel

Reputation: 654

how to give multiple conditions in actionView for render in yii

i have a module that have different render with different conditions so i have used like

    if($p_id != '') {
        $this->render('view', array(
          'model' => $this->loadModel($id, 'Supplier'),
          'modeln' => $this->loadModel($p_id, 'Permit'),
        ));
    } else {
        $this->render('view', array(
          'model' => $this->loadModel($id, 'Supplier'),
        ));

it works fine but i have 5 to 6 conditions like this ,so how can i handle this? any easy way than this? thanks

Upvotes: 1

Views: 133

Answers (2)

tnchalise
tnchalise

Reputation: 1583

There are several approaches.

  1. Switch cases to return the view and model datas as array.
  2. Make $p_id and view name more meaning ful. Or even, as:

    $views = array( 'p_id' => 'corresponding_view file' );

And then later, use it as $this->loadModel($p_id, $views[$p_id]),

Upvotes: 2

hamed
hamed

Reputation: 8043

There are two solutions for this problem:

  1. Adding condition inside controller and rendering multiple views based on condition.(As you do)

  2. Having just one view and adding condition inside the view and showing desired parts of view based on condition.

All these two solution is achievable, But I believe first solution is better. Because second solution has inconsistency with MVC structure. It's not a good idea to put logic inside the view. So I think rendering different views based on condition is better.

Upvotes: 0

Related Questions