Terminate A View Early

How do you terminate a view early but maintain the formatting and layout.

e.g. if I simply call a view from the controller via :$this->render(‘index’);

My View file:




if ($stopHere=='yes'){

//want to terminate here

die; return; end(); //<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />



stuff 2 //should not be rendered if terminating early

Is there a way to do this WITHOUT wrapping the rest of the page in an else{}?

When i die; or Yii::app()->end(); it stops in the correct place but it does not render any of the layout, nav bars etc…

The simple answer is: you should not.

Elaborate your task.

I basically want to check for a condition that rarely happens and if it does to simply terminate the view with a quick message. Would like it to maintain the layout of everything though.

It seems inefficient to create an entirely new view for this simple task.

<? if ($condition): ?>

conditional view

<? else: ?>

rest of the view

<? endif ?>

To be honest, it seems inefficient to code all that logic in the view. It will introduce complexity, code smell, and makes for general debugging much harder. Let your controller make the decision and render the appropriate view.


if ($condition)

        $this->render('generalProfile', array());


        $this->render('advancedProfile', array());


If you really want the logic in the view, I’d recommend breaking it into partialViews.




if (!$stop)