40

I need ERB (Ruby's templating system) for templating of non-HTML files.
(Instead, I want to use it for source files such as .java, .cs, ...)

How do I "execute" Ruby templates from command line?

ivan_ivanovich_ivanoff
  • 19,113
  • 27
  • 81
  • 100

6 Answers6

25

You should have everything you need in your ruby/bin directory. On my (WinXP, Ruby 1.8.6) system, I have ruby/bin/erb.bat

erb.bat [switches] [inputfile]
  -x               print ruby script
  -n               print ruby script with line number
  -v               enable verbose mode
  -d               set $DEBUG to true
  -r [library]     load a library
  -K [kcode]       specify KANJI code-set
  -S [safe_level]  set $SAFE (0..4)
  -T [trim_mode]   specify trim_mode (0..2, -)
  -P               ignore lines which start with "%"

so erb your_erb_file.erb should write the result to STDOUT.

(EDIT: windows has erb.bat and just plain "erb". The .bat file is just a wrapper for erb, which I guess should make the same command work pretty much the same on any OS)

See the prag prog book discussion (starts about half-way down the page).

Note also that Jack Herrington wrote a whole book about code generation that uses Ruby/ERB.

Mike Woodhouse
  • 51,832
  • 12
  • 88
  • 127
  • 1
    Great! I **was** trying to run erb (without knowing if it's correct) but failed, so I thought that it can't be done directly, but need some external library instead. After your reply I realized, that I only had a custom ruby installed, which wasn't in system's path ;) Thank you! – ivan_ivanovich_ivanoff Jun 11 '09 at 11:49
  • I need it for an ant script (it has to run on all platforms). I noticed that here under linux I have erb (without extension), but you've mentioned erb.bat (so I assume under windows you don't have erb without extension). Can you suggest me how to start erb in a multi-platform way? – ivan_ivanovich_ivanoff Jun 11 '09 at 11:55
  • This, and you can inject variables like a boss: erb <(echo -n '<% somevar="yay" %>';cat your.erb) – AXE Labs Jun 23 '16 at 20:21
24

Write a ruby script that does it. The API documentation is here: http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/

For example:

template = ERB.new File.read("path/to/template.erb"), nil, "%"
template.result(binding)

(Where binding is a binding with the @vars that the template needs.)

Ahmed Al Hafoudh
  • 8,281
  • 1
  • 18
  • 34
Sam
  • 1,014
  • 6
  • 8
  • Warning - some Ruby versions do not support various features of Rails ERB (e.g. omit blank lines endings). – lzap Dec 20 '13 at 14:21
15

Another option would be to use ruby -e, since ERB itslef is so simple.

Something like:

ruby -rerb -e "puts ERB.new(File.read(<file name here>)).result"

However, I assume you have a context you want to render the template in. How are you expecting to get that context? As an example, check out:

ruby -rerb -e "hello = 'hello'; puts ERB.new('<%= hello %> world').result(binding)"

which will print out "hello world", using the top-level, where you defined the hello variable, as the binding.

Yehuda Katz
  • 28,535
  • 12
  • 89
  • 91
12

If you can switch ERB to Erubis, your problem solving is as simple as:

require 'erubis'
template = File.read("sample_file.erb")
template = Erubis::Eruby.new(template)
template.result(:your_variable => "sample")
morgoth
  • 1,357
  • 1
  • 13
  • 21
7

Found this question while trying to test my Puppet templates.

Ended with this solution:

  1. Along your foo.erb create a file foo.vars.erb
  2. Put all your template variables into that new file, e.g.:

    <% @my_param="foo bar" %>
    <% @another_param=123 %>
    

    or (equivalent):

    <%
    @my_param="foo bar"
    @another_param=123
    %>
    
  3. On command line run this:

    cat foo.vars.erb foo.erb | erb
    

Your fully rendered template should now be printed to std-out. From there you check the output by hand, or you can take diff (or other tools) to compare it to a pre-rendered output.

Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74
  • Perfect/thanks. This is exactly the use-case I was looking for (puppet testing) – JDS Jan 23 '17 at 19:22
-1

I tried to comment on this, but comments link not available.

I'm using this:

template = ERB.new File.new("path/to/template.erb").read, nil, "%"
template.result(binding)

From the posting above: and I found what I think it might be a problem:

I'm creating DOS BATCH files like:

%JAVA_HOME%\bin\jar -xvf <%=inputfile%>...

And I found weird thing problem - I get this when I run with the code above:

Processing Template test.txt
erb):2:in `render': compile error (SyntaxError)
erb):2: syntax error, unexpected tSTRING_BEG, expecting $end
erbout.concat "\n"
               ^
       from DBUser.rb:49:in `render'
       from DBUser.rb:43:in `each'
       from DBUser.rb:43:in `render'
       from DBUser.rb:81

I tried the following, and got round my particular problem - not sure if this is the right answer for everybody ...

template = ERB.new File.new("path/to/template.erb").read
template.result(binding)
monojohnny
  • 5,894
  • 16
  • 59
  • 83