Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

Can some one please describe the usage of the following characters which is used in ERB file:

<%   %> <%=  %> <%  -%> <%#  %> 

what's the usage of each one ?

like image 335
simo Avatar asked Nov 03 '11 14:11

simo


People also ask

What is <% %> used for?

<% ... %> is used to embed some java code within the main service() method of the JSP. It is executed during the rendering of the page.

What does <% %> mean in EJS?

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.

What is <% in HTML?

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.


2 Answers

<% %> 

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.

like image 104
auralbee Avatar answered Oct 02 '22 08:10

auralbee


<% %> 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

like image 41
yalestar Avatar answered Oct 02 '22 07:10

yalestar