Acces rules for render partial

Hi,

I’m having a guestbook view where I want to use render partial to render the comment form only if someone is logged in.

What’s the best way to do this?

The only way I can think of is checking it in the guestbook view but that doesn’t seem the proper way to me.


<?php if(isset(Yii->app()->user->id)) : ?>

view code

<?php else : ?>

muhaha

<?php endif; ?>

Alternatively:


<?php if(!Yii->app()->user->isGuest) : ?>

view code

<?php else : ?>

muhaha

<?php endif; ?>

yes I know how to check if someone is logged in, but I just wanted to know if it’s the proper way to test it in the view

Yes it is certainly a very valid method and used frequently. Even in your menu items you’ll frequently see checks against Yii::app()->user->isGuest used in the ‘visibility’ param.

Other options if you really want to get it out of the view file:

  1. Have a view for logged in and a view for guest and do the check in the controller before calling the first view. Depends on how important it is for you to keep that logic out of the view. Either way you’re going to end up pulling in a partial file for consistency.

  2. Have a ‘add comment’ widget and put the check inside the widget so that it only renders content if the check is passed.

Putting the logic in a widget is a very good idea. Thumbs up! :)

ok, thanks for your help :)