Created
April 16, 2018 18:35
-
-
Save pruthvi6767/48ecfdea970b5e28f76eddcb4468959f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Generates an html page using ERB | |
require 'erb' | |
*Initializes the key,val objects to bind the erb templates | |
class BindMe | |
attr_accessor :key | |
attr_accessor :val | |
def initialize(key,val) | |
@key=key | |
@val=val | |
end | |
def get_binding | |
return binding() | |
end | |
end | |
* hash parameter | |
items = {'A' => 1, 'B' => 2, 'C' => 3} | |
arr= [] | |
* Generating objects to bind each object with ERB template | |
items.each do |key,value| | |
#binding.pry | |
bind_me = BindMe.new(key.to_s,value) | |
arr.push(bind_me) | |
end | |
* Template to be used | |
template = ''' | |
<html> | |
<head> | |
</head> | |
<body> | |
<table> | |
<tr> | |
<td> <h1> KEY </h1> </td> | |
<td> <h1> VALUE </h1> </td> | |
</tr> | |
<tr> | |
<td> <h3> Name:<%= key %> </h3> </td> | |
<td> <h3> value: <%= val %> </h3> </td> | |
</tr> | |
</table> | |
</body> | |
</html> | |
''' | |
puts arr | |
* loops in on the array objects | |
Here it would be useful to pass the arr objects as params with ERB | |
to template and use ruby code in template to loop in and gen. | |
complete html in one call | |
arr.each do |ele| | |
#binding.pry | |
result = ERB.new(template).result(ele.get_binding) | |
puts result | |
open('index2.html', 'w+') { |f| | |
f.puts result | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment