Can some one please describe the usage of the following characters which is used in ERB file:
<% %> <%= %> <% -%> <%# %>
what's the usage of each one ?
<% ... %> is used to embed some java code within the main service() method of the JSP. It is executed during the rendering of the page.
The following is from ejs docs (tag section): <% 'Scriptlet' tag, for control-flow, no output. <%= Outputs the value into the template (HTML escaped) <%- Outputs the unescaped value into the template.
Summary. The new <%: %> syntax provides a concise way to automatically HTML encode content and then render it as output. It allows you to make your code a little less verbose, and to easily check/verify that you are always HTML encoding content throughout your site.
<% %>
Executes the ruby code within the brackets.
<%= %>
Prints something into erb file.
<%== %>
Equivalent to <%= raw %>
. Prints something verbatim (i.e. w/o escaping) into erb file. (Taken from Ruby on Rails Guides.)
<% -%>
Avoids line break after expression.
<%# %>
Comments out code within brackets; not sent to client (as opposed to HTML comments).
Visit Ruby Doc for more infos about ERB.
<% %>
and <%- and -%>
are for any Ruby code, but doesn't output the results (e.g. if statements). the two are the same.
<%= %>
is for outputting the results of Ruby code
<%# %>
is an ERB comment
Here's a good guide: http://api.rubyonrails.org/classes/ActionView/Base.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With