Skip to content

Instantly share code, notes, and snippets.

@pruthvi6767
Created April 16, 2018 18:35
Show Gist options
  • Save pruthvi6767/48ecfdea970b5e28f76eddcb4468959f to your computer and use it in GitHub Desktop.
Save pruthvi6767/48ecfdea970b5e28f76eddcb4468959f to your computer and use it in GitHub Desktop.
## 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