
Now he has the costume on, but not happy about it.

Definitely not happy!

<email>
<to><%= $name %></to>
<from><%= $me %></from>
<date><%= Time.now %></date>
<subject><%= $hello %></subject>
On your way home, please pick up the following from the store:
% $list.each do |thing|
* <%= thing %>
% end
<%= $signoff %>
</email>
<%=
and %>
. Anything inside these tags are treated like a Ruby expression and are evaluated. I also have two occurrences of just %
. These are treated as the other tags if they are at the start of the line. (Note that I did not indent those lines!) This is useful for us because we cannot nest the <%=
and %>
tags. (Try it!)require 'rubygems'
require 'erb'
# set up some variables that we want to replace in the template
$hello = "Hola"
$me = "Tu mama"
$name = "Mi'jito"
$list = [ "milk", "eggs", "bread"]
$signoff = "Te quiero mucho."
# method update_tokens takes template_file, expecting globals
# to be set, and will return an updated string with tokens replaced.
# you can either save to a new file, or output to the user some
# other way.
def update_tokens(template_file)
template = ""
open(template_file) {|f|
template = f.to_a.join
}
updated = ERB.new(template, 0, "%<>").result
return updated
end
new_xml=update_tokens(Dir.getwd+"/ERB_example.xml")
puts new_xml
%
says to process any %
at the beginning of a line. Second, the <>
says to omit any newline for lines beginning with <%
and ending with %>
.<email>
<to>Mi'jito</to>
<from>Tu mama</from>
<date>Tue Oct 07 14:22:10 -0500 2008</date>
<subject>Hola</subject>
On your way home, please pick up the following from the store:
* milk
* eggs
* bread
Te quiero mucho.
</email>
export RUBYLIB=/opt/ruby/lib:/usr/lib/site_ruby
export PATH=$PATH:/opt/ruby:/opt/ruby/bin
wget ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.6.tar.gz
tar -zxvf ruby-1.8.6.tar.gz
ln -s ruby-1.8.6-p287 ruby
cd ruby
./configure
make
make install
wget http://rubyforge.org/frs/download.php/43985/rubygems-1.3.0.tgz
tar -zxvf rubygems-1.3.0.tgz
cd rubygems-1.3.0
ruby setup.rb