是否有更简洁的语法在erb中显示变量?

时间:2010-06-04 07:02:55

标签: ruby syntax erb

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

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

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

  

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

     

&lt;%Ruby代码 - 内联输出%&gt;
    &lt;%= Ruby表达式 - 替换为结果%&gt;
    &lt;%#comment - ignored - 在测试%&gt;时很有用     %一行Ruby代码 - 被视为&lt;%line%&gt; (可选 - 见ERB.new)
    如果行上的第一件事和%处理使用了,则%%替换为%     &lt; %%或%%&gt; - 替换为&lt;%或%&gt;分别
    所有其他文本都通过ERB过滤未更改

相关问题