Print in ERB without <%=?

前端 未结 3 1273
萌比男神i
萌比男神i 2020-12-14 00:57

Sometimes it\'s more convenient to print in <%%>. How to do it in Rails?

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-14 01:54

    In ERB: The <% %> signify that there is Ruby code here to be interpreted. The <%= %> says output the ruby code, ie display/print the result.

    So it seems you need to use the extra = sign if you want to output in a standard ERB file.

    Otherwise, you could look at alternatives to ERB which require less syntax,.. maybe try something like HAML. http://haml-lang.com/tutorial.html

    Example:
    
    # ERB
    <%= item.title %>
    
    # HAML
    %strong= item.title
    

    Is that more convenient?

提交回复
热议问题