【问题标题】:Is there a neater syntax for displaying a variable in erb?在 erb 中显示变量是否有更简洁的语法?
【发布时间】:2010-06-04 07:02:55
【问题描述】:

我是 Ruby 新手,我发现 <%= @my_variable %> 有点冗长且可读性差。 erb 不应该有 ${@my_variable} 之类的东西(就像 Java 中的老式 Velocity 引擎)吗?

【问题讨论】:

    标签: ruby syntax erb


    【解决方案1】:

    erb,顾名思义,用于嵌入式 ruby​​ 代码。除了以下公认的标签,我认为它没有任何其他语法:

    <% 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
    

    如果您不喜欢 erb 语法,还有其他用于 Ruby 的替代模板引擎。看看HamlLiquidMustacheTenjin

    【讨论】:

      【解决方案2】:

      根据ERB documentation,我猜答案是否定的。

      ERB 识别提供的模板中的某些标签并根据以下规则对其进行转换:




      % 一行 Ruby 代码 -- 被视为 (可选 -- 参见 ERB.new)
      %% 替换为 % 如果首先在一行中使用 % 处理
      -- 分别替换为
      所有其他文本均未更改地通过 ERB 过滤

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-27
        • 2016-07-01
        • 1970-01-01
        • 2011-11-17
        • 1970-01-01
        • 2014-06-07
        • 1970-01-01
        相关资源
        最近更新 更多