In asp.net mvc, when do we use:
and
Do we ever need to put a ; (colon) ?
<%= %> renders the output (string) of the contained command to the response. <% %> wraps executable statements (logic) in the view to control what gets executed. You don't use semicolons in the <%= %> blocks, but may in the <% %> depending on what statements are included.
String rendering:
<%= Html.Encode( Model. Property ) %>
Code block:
<% Html.RenderPartial( "ViewName" ); %>
EDIT: Here's a link to the reference.
<%="something" %> is just a shortcut for Response.Write("something")
is used when you are calling some HtmlHelper method which returns a string e.g.:
is used when you are calling some HtmlHelper method which is void:
Related
I am trying to pass a string to my view from controller like this:
controller:
def index
#str = 'foo'
end
view:
String: <% #str %>
The variable itself seems to arrive because I get no error. However, it arrives empty (only "String" is in html, nothing else). And it seems to work great with other built-in types, e.g. Time. What am I missing here? I use Ruby 2.2.1 and Rails 4.
As others have said, you need to use
<%= #str %>
I'll give you an explanation as well - you use <% %> for when you need to run some Ruby code that you don't want displayed to the screen. For example, you might have conditional logic like
<% if user_signed_in? %>
<%= #welcome_string %>
<% end %>
Use <%= %> when you want to output, drop the '=' for conditional logic or anything that doesn't need to display.
in your view
String: <%= #str %>
In view user following code:
String: <%= #str %>
In your view, use:
<%= #str %>
As the other users have pointed out, you need to use <%=
The = is an ERB flag to so export the result of the code inside of the tags and put it into the DOM.
If you want to put some logic into your page that you don't want to evaluate, you leave the = out.
<% if user_wants_to_see_output? %>
<%= "User will see this" %>
<% end %>
I was trying to refactor my erb code for rating stars into one line and came across this, is it possible to achieve the results another way?
So currently it's like this
<% rating.times do %>
<%= image_tag 'rating_star.png' %>
<% end %>
Ideally I'd like to reduce this to
<%= rating.times { image_tag 'rating_star.png' } %>
This returns the value of rating (0, 1, 2, 3, 4 or 5), if I change <%= to <% nothing is rendered. Is there any way to do this?
You can use concat:
<% rating.times { concat image_tag('rating_star.png') } %>
But <%= ... %> within a loop is cleaner IMO.
You need #map to return an array of all image_tags you wish.
Then #join the result to get a string.
Lastly, you need to tell this string is valid html with #html_safe.
<%= rating.times.map{ image_tag 'rating_star.png' }.join.html_safe %>
The difference between <% %> and <%= %> is that the former is simply evaluated, but with the latter the result is rendered. You have two things going on in your code snippet: the control part (the loop), and the rendering part (the image tag). Since you want only the image tag part rendered, you want to surround only that code with <=% %>, and the rest with <% %>.
So while this would work:
<%= rating.times { %> <%= image_tag 'rating_star.png' %> <% } %>
I find it much less readable than the 3-line version.
I used this gem in my application, but I'm not sure the difference between the different implementation options for the gem:
form_for
form_tag with block
form_tag without block
Can anyone clarify? I understand that form_for is used when you wish to interact with a model, but what about the other two?
The differences are subtle, but important. form_for is a more advanced tool that yields an object you use to generate your form elements:
<% form_for(#foo) do |form| %>
<%= form.text_field(:bar) %>
<% end %>
The form_tag method is much more primitive and just emits a tag. If you want to put things inside of the <form> tag that's emitted, you put things inside the block:
<% form_tag do %>
<%= text_field_tag(:bar, 'bar_value') %>
<% end %>
Note that the form_for method handles grabbing values from your model, and will at least try to route the form to the appropriate action. With form_tag you are responsible for everything as it makes no assumptions about what you're doing.
One uses model binding and the other doesn't
As far as I know there is only one simple difference. form_tag without a block will only generate a html element for you. When you use form with a a block it will also create the form closing tag .
In example:
<% form_tag("/comments") %>
will result in
<form action="/comments">
Where
<%= form_tag("/comments") do %>
<%= submit_tag %>
<% end %>
will generate
<form action="/comments">
<input type="sumbit" />
</form>
In reference to this
I've created a question in a webform like this:
<div class="form_row">
<label for="features[]">Features:</label>
<% [ 'scenarios', 'role_profiles', 'private_messages', 'polls' ].each do |feature| %>
<br><%= check_box_tag 'features[]', feature,
(params[:features] || {}).include?(feature) %>
<% end %>
</div>
So if scenarios and private_messages gets checked and I print out params[:features] I will get:
scenariosprivate_messages
I was wondering how would I be able to obtain scenarios and private_messages separately from params. Is the mapping params[:features] = "scenariosprivate_messages" or is it really params[features] = ["scenarios", "private_messages"] ? If it's the latter how can I loop through them?
I write in my view:
<%= params[:features].each {|param|
param.capitalize
} %>
and I still just get scenariosprivate_messages printed.
Try this instead:
<% params[:features].each do |param| %>
<%= param.capitalize %>
<% end %>
The problem with your original solution is that you're printing out the result of the block, which is the array itself, rather than printing out each element of the array.
You shouldn't be using params in your views. You're best off assigning params[:features] to an instance variable in your controller and then iterating over that in your view.
But to answer your question, you're putting the equals sign for output in the wrong place. You want to output each element of the array individually instead of outputting the result of the loop.
You must use humanize:
<% params[:features].each do |param| %>
<%= param.humanize %>
<% end %>
According to this blog post you should be able to access them individually as params[:features]['scenarios'] etc. Looping should just work like with all other arrays -- eg
params[:features].each { |param|
# do something with param
}
I want to do a conditional rendering at the layout level based on the actual template has defined content_for(:an__area), any idea how to get this done?
#content_for_whatever is deprecated.
Use content_for? instead, like this:
<% if content_for?(:whatever) %>
<div><%= yield(:whatever) %></div>
<% end %>
not really necessary to create a helper method:
<% if #content_for_sidebar %>
<div id="sidebar">
<%= yield :sidebar %>
</div>
<% end %>
then of course in your view:
<% content_for :sidebar do %>
...
<% end %>
I use this all the time to conditionally go between a one column and two column layout
<%if content_for?(:content)%>
<%= yield(:content) %>
<%end%>
Can create a helper:
def content_defined?(var)
content_var_name="#content_for_#{var}"
!instance_variable_get(content_var_name).nil?
end
And use this in your layout:
<% if content_defined?(:an__area) %>
<h1>An area is defined: <%= yield :an__area %></h1>
<% end %>
Ok I am going to shamelessly do a self reply as no one has answered and I have already found the answer :)
Define this as a helper method either in application_helper.rb or anywhere you found convenient.
def content_defined?(symbol)
content_var_name="#content_for_" +
if symbol.kind_of? Symbol
symbol.to_s
elsif symbol.kind_of? String
symbol
else
raise "Parameter symbol must be string or symbol"
end
!instance_variable_get(content_var_name).nil?
end
I'm not sure of the performance implications of calling yield twice, but this will do regardless of the internal implementation of yield (#content_for_xyz is deprecated) and without any extra code or helper methods:
<% if yield :sidebar %>
<div id="sidebar">
<%= yield :sidebar %>
</div>
<% end %>
I use #view_flow and value of the content method before checking if the content is present in the view like this:
#view_flow.content[:header_left_or_whatever_the_name_of_your_block_is].present?
Recently stumbled upon it when showing all local, global and instance variables of self in the console with byebug. I’m a fan using this because it’s straight from Rails, won’t throw an error, won’t hide anything w “Rails magic”, returns a definite true or false, + only checks the content in the current context of the view being rendered.
#view_flow is an instance attribute of ActionView::Context and because Action View contexts are supplied to Action Controller to render a template it will be available to any view that has been rendered by Rails. Although it checks for content, the content_for block will not be yielded if it isn’t there. So it’s been my perfect solution in similar situations.