scott_fakename scott_fakename - 8 months ago 43
Ruby Question

Passing binding or arguments to ERB from the command line

I have been playing around with erb from the command line recently. I wanted to make a dirt simple erb template, for example the following:

<%- name = "Joe"; quality = "fantastic" -%>
Hello. My name is <%= name %>. I hope your day is <%= quality %>.

This works if I run

erb -T - thatfile.erb

what I want to do is to make
be passable from command line arguments, so that I could do something like:

./thatfile.erb "Bill" "super"

from the bash prompt and do the same thing.

I am aware that I could write a ruby script that would just read that template in and then use"thatfile.erb")).result(binding)
, or writing the template after an
and doing likewise, but I'm looking for a more lightweight approach if it exists, because I don't want to write two files for each erb script that I create for this purpose.


If you are using unix, try following:

$ cat 1.erb
Hello. My name is <%= name %>. I hope your day is <%= quality %>.
$ (echo '<% name="Joe"; quality="fantastic" %>' && cat 1.erb) | erb

Hello. My name is Joe. I hope your day is fantastic.