在使用Ruby on Rails时,我总是使用<%= some_code %>
将Ruby插入到HTML中。我刚刚注意到其他项目有时使用<%= some_code -%>
。在Ruby on Rails中, - %>意味着什么,而%>
7
A
回答
13
<%= some_code - %>最后的减号删除换行符。用于格式化生成的HTML,而<%= some_code%>不。
感谢,Anubhaw
+0
那么从' <%='vs'<%'? – Shawn 2011-09-15 04:22:59
+0
@ShawnSep'='添加输出,'%'不会用于像条件'<% if %>'这样的东西。 – 2014-09-01 20:21:39
4
1
这个答案是错误的:请参阅https://stackoverflow.com/a/25617607/895245。
在Ruby 2.1(不一定与滑轨),则-
移除一个以下换行符作为指出通过Anubhaw:
- 换行符必须是
>
- 没有空格是后的第一个字符删除
- 只删除一个换行
- 你必须通过
'-'
选择使用它
例子:
require 'erb'
ERB.new("<%= 'a' %>\nb").result == "a\nb" or raise
begin ERB.new("<%= 'a' -%>\nb").result; rescue SyntaxError ; else raise; end
ERB.new("<%= 'a' %>\nb" , nil, '-').result == "a\nb" or raise
ERB.new("<%= 'a' -%>\nb" , nil, '-').result == 'ab' or raise
ERB.new("<%= 'a' -%> \nb" , nil, '-').result == "a \nb" or raise
ERB.new("<%= 'a' -%>\n b" , nil, '-').result == 'a b' or raise
ERB.new("<%= 'a' -%>\n\nb", nil, '-').result == "a\nb" or raise
文件:http://ruby-doc.org/stdlib-2.1.1/libdoc/erb/rdoc/ERB.html
的Rails 4.1的似乎:
超集的问题,所有格式''<% %>修饰了后者:http://stackoverflow.com/questions/7996695/rails-erb-syntax – 2014-09-01 21:09:19
@CiroSantilli:你”重新统治这些 - 好的工作!我选择了http://stackoverflow.com/questions/7996695/what-is-the-difference-between-and-in-erb-in-rails这个也是... – 2014-09-02 04:50:59