4

If I have an .html.erb file that looks like this:

<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <%= @name %>
    </body>
</html>

How can I generate an HTML file that looks like this?

<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        John
    </body>
</html>

How can I execute the template (passing the name parameter) given that the format is .html.erb and be able to get just an .html file?

J148
  • 576
  • 1
  • 4
  • 17
blackghost
  • 683
  • 1
  • 8
  • 18

3 Answers3

14
#page.html.erb
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <%= @name %>
    </body>
</html>

...

require 'erb'

erb_file = 'page.html.erb'
html_file = File.basename(erb_file, '.erb') #=>"page.html"

erb_str = File.read(erb_file)

@name = "John"
renderer = ERB.new(erb_str)
result = renderer.result()

File.open(html_file, 'w') do |f|
  f.write(result)
end

...

$ ruby erb_prog.rb
$ cat page.html
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        John
    </body>
</html>

Of course, to make things more interesting, you could always change the line @name = "John" to:

print "Enter a name: "
@name = gets.chomp
7stud
  • 46,922
  • 14
  • 101
  • 127
  • if you need a binding to get the instance variable readable from the template, and are using Ruby 2.1+, check here: http://stackoverflow.com/a/28006697/626369 – Dominick Apr 08 '16 at 20:19
0

The ruby ERB library operates on strings, so you would have to read the .erb file into a string, create an ERB object, and then output the result into an html file, passing the current binding to the ERB.result method.

It would look something like

my_html_str = ERB.new(@my_erb_string).result(binding)

where @my_erb_string. You could then write the html string into an html file.

You can read more about binding here and about the ERB library here.

eugmill
  • 301
  • 2
  • 7
  • I have two questions: How can I write that result into an HTML file, and how can I make sure that the binding has @name defined? – blackghost Aug 12 '14 at 22:36
  • @blackghost Html is just a normal text file, so basic IO stuff still works: [link](http://stackoverflow.com/questions/2777802/how-to-write-to-file-in-ruby) As for binding, the binding() method returns a binding object, which captures the environment it's called in. So if `@name` is defined in the same context that binding is called, it will be accessible in the erb. – eugmill Aug 12 '14 at 22:43
0

Here is a sample code:

require 'erb'

template = File.read("template.html.erb")

renderer = ERB.new(template)

class Message
  attr_accessor :name

  def initialize(name)
    @name = name
  end

  def get_binding
    binding()
  end

end

message = Message.new 'John'

File.open('result.html', 'w') do |f|
  f.write renderer.result message.get_binding
end

The template.html.erb is your .html.erb template file and result.html is the rendered html file.

Some nice articles you can take a look, link1, link2.

Wenbing Li
  • 12,289
  • 1
  • 29
  • 41