开发者

Is there a neater syntax for displaying a variable in erb?

开发者 https://www.devze.com 2023-01-02 10:46 出处:网络
I\'m new to Ruby, and I found the <%= @my_variable %> a little lengthy and less readable. Shouldn\'t erb have something like ${@my_variable} (like the good old Veloc开发者_StackOverflow中文版ity

I'm new to Ruby, and I found the <%= @my_variable %> a little lengthy and less readable. Shouldn't erb have something like ${@my_variable} (like the good old Veloc开发者_StackOverflow中文版ity engine in Java)?


erb, as its name suggests, is intended for embedded ruby code. I don't think it has any other syntax beyond the following recognised tags:

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

If you don't like erb syntax there are other alternative template engines for Ruby. Take a look at Haml, Liquid, Mustache or Tenjin.


I guess the answer is no according to the ERB documentation.

ERB recognizes certain tags in the provided template and converts them based on the rules below:

<% 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
All other text is passed through ERB filtering unchanged.

0

精彩评论

暂无评论...
验证码 换一张
取 消