views:

173

answers:

2

How do people generally handle conditional statements in MVC frameworks

For some of my pages (made under Kohana) I want the user to have more options depending on whether they are logged in or not, and whether the profile is their own profile for example. From your own experience, do you create separate views, empty variables, do the logic in the view or what?

Thanks

Zenna

+1  A: 

This depends on how different the view will look like based on the values of the variable. For example, if you need to just hide / show a couple controls, I'd include controls into the view that would be empty in some cases, and contain data in others.

If it's more than a couple controls are shown/hidden at once, and your framework supports a concept of "panels" (reusable parts of the view), I'd separate that group of controls into a panel and show/hide that panel depending on the conditional.

The tradeoff here is in "cleanliness" (do you have intermingled view components for two views in one?) versus "ease of updateability" (if both views are co-evolving, do you update two places or just one?)

Alex
+2  A: 

For me personally I will pass information over to the view to dictate what pieces of the view to display, as I consider showing/hiding display elements a piece of view logic.

In the case of Kohana, if you were to pass a $user variable to the view that is a User_Model object, even if a person isn't loaded the object will still exist, so you can write code in your view such as:

<?php if ($user->logged_in()):?>
<div>Some login only markup here</div>
<?php else:?>
<div>some not login only markup here</div>
<?php endif;?>

Your mileage may also vary depending on if you're using ORM or another ORM-like library instead of writing models by scratch.

Nodren