zoukankan      html  css  js  c++  java
  • 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' %>
    b").result              == "a
    b"  or raise
    begin ERB.new("<%= 'a' -%>
    b").result; rescue SyntaxError ; else raise; end
    ERB.new("<%= 'a'  %>
    b"  , nil, '-').result == "a
    b"  or raise
    ERB.new("<%= 'a' -%>
    b"  , nil, '-').result == 'ab'    or raise
    ERB.new("<%= 'a' -%> 
    b" , nil, '-').result == "a 
    b" or raise
    ERB.new("<%= 'a' -%>
     b" , nil, '-').result == 'a b'   or raise
    ERB.new("<%= 'a' -%>
    
    b", nil, '-').result == "a
    b"  or raise
  • 相关阅读:
    strut2 国际化
    strut2 常量
    strut2 自定义类型转换器
    strut2基于XML配置方式对Action中的指定方法校验
    strut2 输入校验2
    strut2 输入校验
    strut2 模拟拦截器
    strut2 多个文件上传
    strut2 单个文件上传
    2015.01.01今年的第一天
  • 原文地址:https://www.cnblogs.com/or2-/p/5222130.html
Copyright © 2011-2022 走看看