强大的ruby模版:ERB

#http://stdlib.rubyonrails.org/

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

# Create template.
template = %{

Ruby Toys -- <%= @name %>


<%= @name %> (<%= @code %>)


<%= @desc %>




    <% @features.each do |f| %>
  • <%= f %>

  • <% end %>



<% if @cost < 10 %>
Only <%= @cost %>!!!
<% else %>
Call for a price, today!
<% end %>





}.gsub(/^ /, '')

rhtml = ERB.new(template)

# Set up template data.
toy = Product.new( "TZ-1002",
"Rubysapien",
"Geek's Best Friend! Responds to Ruby commands...",
999.95 )
toy.add_feature("Listens for verbal commands in the Ruby language!")
toy.add_feature("Ignores Perl, Java, and all C variants.")
toy.add_feature("Karate-Chop Action!!!")
toy.add_feature("Matz signature on left leg.")
toy.add_feature("Gem studded eyes... Rubies, of course!")

# Produce result.
rhtml.run(toy.get_binding)

你可能感兴趣的:(Ruby,&&,RoR)