Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it acceptable keep logic in view templates?

Is it acceptable keep logic in view templates (in MVC terms and Rails specifically)? Is there any way to avoid something like this?

<% if current_user %>
  Welcome, <%= current_user.name %>.
  <%= link_to "Sign Out", signout_path %>
<% else %>
  <%= link_to "Sign in with Twitter", "/auth/twitter" %>
<% end %>

Or am I inventing a "bicycle" and the upper stuff goes well?

like image 856
kyrylo Avatar asked Dec 16 '22 19:12

kyrylo


1 Answers

Comes down to preference of course. I would say that the above is pretty acceptable. Simple conditions and loops are pretty standard in a view. I would say the things to avoid would be assigning variables, hitting models for data you haven't already gathered, etc.

If you don't want anything like tha in there, you can always build helpers. This kind of thing has to be somewhere.

edit:

a good rule of thumb is "does this code directly relate to the presentation?" I would say the answer regarding your above case is yes.

like image 73
re5et Avatar answered Dec 30 '22 09:12

re5et