【发布时间】:2010-06-04 07:02:55
【问题描述】:
我是 Ruby 新手,我发现 <%= @my_variable %> 有点冗长且可读性差。 erb 不应该有 ${@my_variable} 之类的东西(就像 Java 中的老式 Velocity 引擎)吗?
【问题讨论】:
我是 Ruby 新手,我发现 <%= @my_variable %> 有点冗长且可读性差。 erb 不应该有 ${@my_variable} 之类的东西(就像 Java 中的老式 Velocity 引擎)吗?
【问题讨论】:
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 的替代模板引擎。看看Haml、Liquid、Mustache 或Tenjin。
【讨论】:
根据ERB documentation,我猜答案是否定的。
ERB 识别提供的模板中的某些标签并根据以下规则对其进行转换:
% 一行 Ruby 代码 -- 被视为 (可选 -- 参见 ERB.new)
%% 替换为 % 如果首先在一行中使用 % 处理
-- 分别替换为
所有其他文本均未更改地通过 ERB 过滤。
【讨论】: