Embedded ruby “erb” tags [duplicate]

五迷三道 提交于 2020-01-03 15:58:52

问题


I have been going through a crash course of Ruby and Ruby on Rails and i can't figure this out: In embedded ruby html files, there are several tags. <% %> for execution <%= %> for output, but what function do these tags serve: <%= -%>, what's with the "-" sign at the end ?

Thanks.


回答1:


This link contains a good overview of erb markup.

From the site:

RECOGNIZED TAGS

ERB recognizes certain tags in the provided template and converts
them based on the rules below:

<% Ruby code -- inline with output %>

<%= Ruby expression -- replace with result %>

<%# comment -- ignored -- useful in testing %>

% a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)

%% replaced with % if first thing on a line and % processing is used

<%% or %%> -- replace with <% or %> respectively

All other text is passed through ERB filtering unchanged.




回答2:


In ruby document there is not the details about this usage <%- or -%>. But it works well by this:

erbA = ERB.new(erbA_str, 1, '-', "@output_buffer")

Notice the 3rd parameter '-' ! It removes the pre or post blanks when using <%- and -%> accordingly.



来源:https://stackoverflow.com/questions/9208728/embedded-ruby-erb-tags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!