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?

1

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

1 Comment

if you need a binding to get the instance variable readable from the template, and are using Ruby 2.1+, check here: stackoverflow.com/a/28006697/626369
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.

2 Comments

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 Html is just a normal text file, so basic IO stuff still works: link 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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.