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

前端 未结 7 1651
一向
一向 2020-11-21 05:33

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

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


        
7条回答
  •  别那么骄傲
    2020-11-21 05:56

    These are use in ruby on rails :-

    <% %> :-

    The <% %> tags are used to execute Ruby code that does not return anything, such as conditions, loops or blocks. Eg :-

    Names of all the people

    <% @people.each do |person| %> Name: <%= person.name %>
    <% end %>

    <%= %> :-

    use to display the content .

    Name: <%= person.name %>

    <% -%>:-

    Rails extends ERB, so that you can suppress the newline simply by adding a trailing hyphen to tags in Rails templates

    <%# %>:-

    comment out the code

    <%# WRONG %>
    Hi, Mr. <% puts "Frodo" %>
    

提交回复
热议问题