RoR: What are the benefits of erb in Rails code?

I read Magnus Holm’s impressive post titled Helper Assistants in Rails 3 , in which he points out that Rails 3 has bent the ERB syntax too far. span multiple operators.)

Which makes me wonder: what are the real benefits of using ERB over Ruby's own string processing? To understand this, I took the example given in the ERB documentation and tried it both in ERB and in my own Ruby lines. It turns out that the rich Ruby string processing library makes translation really simple - even intuitive.

Here is how it looks. Common to both (taken directly from the ERB documentation):

require "erb"

# Build template data class.
class Product
  def initialize( code, name, desc, cost )
    @code = code
    @name = name
    @desc = desc
    @cost = cost
    @features = [ ]
  end

  def add_feature( feature )
    @features << feature
  end

  # Support templating of member data.
  def get_binding
    binding
  end
end

Here is the template and extension written in ERB:

# ================================================================
# using ERB
erb_template = %{
    <html>
      <head><title>Ruby Toys -- <%= @name %></title></head>
      <body>
        <h1><%= @name %> (<%= @code %>)</h1>
        <p><%= @desc %></p>
        <ul>
          <% @features.each do |f| %>
            <li><b><%= f %></b></li>
          <% end %>
        </ul>
        <p>
          <% if @cost < 10 %>
            <b>Only <%= @cost %>!!!</b>
          <% else %>
             Call for a price, today!
          <% end %>
        </p>
      </body>
    </html>
  }.gsub(/^  /, '')
rhtml = ERB.new(erb_template)
# Produce results
@r1 = rhtml.result(toy.get_binding)

And here is a template written in pure Ruby:

# ================================================================
# using native Ruby strings
ruby_template = %q{"
    <html>
      <head><title>Ruby Toys -- #{ @name }</title></head>
      <body>
        <h1>#{ @name } (#{ @code })</h1>
        <p>#{ @desc }</p>
        <ul>
          #{ @features.map do |f|
               "<li><b>#{f}</b></li>\n"
             end.join }
        </ul>
        <p>
          #{ if @cost < 10
               "<b>Only #{ @cost }!!!</b>"
             else
               "Call for a price, today!"
             end
           }
        </p>
      </body>
    </html>
  "}
# Produce results
@r2 = eval(ruby_template, toy.get_binding)

They give the same results (modulo spaces). Whether ERB is simpler or more complex is really a matter of taste and experience. Judging by the number of questions about ERB and <% = ...%> vs <% ...%> vs <% = ... -%>, it seems that it can be easier for many people to stick to time for direct ruby.

At the risk of starting some kind of holy war, why bother with ERB when native Ruby does the same job? Do you find ERB useful? Should Rails accept native Ruby templates?

+3
source share
2 answers

Magnus Hill is here; nice to see that you liked the article :-)

(, - ) :

# "Regular" way of constructing a string:
str = "Hello World: "
@users.each do |user|
  str << "How are you today, "
  str << user.name
  str << "? \n"
end

, , :

  • , : str << "Static"
  • , : str << expresstion
  • , : @users.each

ERB , , , . ( ), .

"", , . : , , Turing, . , .

. , , , . GOTO: / , (GOTO), , if/while-statements. ? ! , , , , .

, , , : @users.map { |f| code }.join. , ERB , .

, , , , . , , , DRY-, .. . , , . , Lisp flavors, , , .

, ERB Ruby? : .

, ERB ?

. , . , , . , ERB , : , :

# This block is more like a statement (we don't care about the return value)
@users.each do |foo|
  str << "Hello"
end

# This block is an expression
str << form_for(thing) do
  another_string
end

, , "" -, . "" .

Rails " Ruby"?

Tilt , , Rails Tilt, , " Ruby" . , , , . , Ruby, , ?

+4

Rails - . "" , , , , -: erb, Test:: Unit, .., ( Rails 3).

-, erb Rails, erb, , ruby ​​- . , HTML .

, haml , erb. . Rails . , , , , .

, " Rails " Ruby "?", . - ;)

+1

Source: https://habr.com/ru/post/1795828/


All Articles