在使用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