Reputation: 3211
Sometimes inside my templates I want to define default values for some CSS properties when those values are not passed as parameters in the render function, like this:
height: <%= if @height do %><%=@height%><% else %>"50px";<%end%>
I'm wondering if there's a better (less verbose) way of doing this.
Upvotes: 2
Views: 578
Reputation: 222148
You can use the fact that
@
variables can also be accessed through a map named assigns
,||
returns the right hand side value if the left one is nil
,and write:
<%= assigns[:height] || "50px" %>
to get the same behavior as your if
/else
.
Edit: if you're always setting @height
, even if the value is nil
, you can do this with even lesser code:
<%= @height || "50px" %>
The previous code will work even if @height
is not set to any value but this will raise an exception if @height
is not set.
Upvotes: 6