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

http://stackoverflow.com/questions/998979/difference-between-and-in-rails/25617607#25617607

http://stackoverflow.com/questions/7996695/what-is-the-difference-between-and-in-erb-in-rails 

<% %>

Executes the ruby code within the brackets.

<%= %>

Prints something into erb file.

<% -%>

Avoids line break after expression.

<%# %>

Comments out code within brackets; not sent to client (as opposed to HTML comments).

 

 

In Ruby 2.1 (not necessarily with Rails), the - removes one trailing newline:

  • the newline must be the first char after the >
  • no spaces are removed
  • only a single newline is removed
  • you must pass the '-' option to use it

Examples:

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

转载于:https://www.cnblogs.com/or2-/p/5222130.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值