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?
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
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.
...
...
Of course, to make things more interesting, you could always change the line
@name = "John"
to:Here is a sample code:
The
template.html.erb
is your .html.erb template file andresult.html
is the rendered html file.Some nice articles you can take a look, link1, link2.