Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating an HTML file with ruby

Tags:

html

ruby

I'm currently creating a report page in HTML format with ruby

An Example is:

fileHtml = File.new("filename.html", "w+")

fileHtml.puts "<HTML>"
fileHtml.puts "<HEAD>"
fileHtml.puts "<style media='all' type='text/css'>"
fileHtml.puts "body {font-family: Helvetica Neue, sans-serif; font-size: 18px; color: #525252; padding: 0; margin: 0;background: #f2f2f2;}"
fileHtml.puts ".content {max-width:1180px; padding: 40px;}"
fileHtml.puts ".div1 {margin-top: 28px; margin-bottom: 1px; background-color: #fff; padding: 10px 40px; padding-bottom: 8px; }"
fileHtml.puts ".div2 {margin-top: 2px; height:25%; margin-bottom: 28px; background-color: #fff; padding: 10px 40px; padding-bottom: 8px; }"
fileHtml.puts ".header {background-color: white; height: 16%; min-height: 110px; position: relative; width: 100%; -webkit-user-select: none;}"
fileHtml.puts ".secondSection {background-color: #e8e8e8; height: 16%; min-height: 110px; position: relative; width: 100%; -webkit-user-select: none;}"
fileHtml.puts ".pass {color: #ffffff; background: #34d9a2; padding: 10px 20px 10px 20px; text-decoration: none; width:50px;}"
fileHtml.puts ".fail {color: #ffffff; background: #f25e6a; padding: 10px 20px 10px 20px; text-decoration: none; width:50px;}"
fileHtml.puts "</style>"
fileHtml.puts "</HEAD>"
fileHtml.puts "<BODY>"
fileHtml.puts "<DIV class='header'><img src='img.png' alt='logo'></div>"
fileHtml.puts "<DIV class='secondSection'><p>#{dateTime}</p><p>#{platform}</p><p>#{log}</p></div>"
fileHtml.puts "<DIV class='content'>"
fileHtml.puts "<DIV class='div1'><h2>Test Case 001: name</h2></DIV>"
fileHtml.puts "<DIV class='div2'><p class='pass'>#{pass}</p><p class='fail'>#{fail}</p></DIV>"
fileHtml.puts "</DIV>"
fileHtml.puts "</BODY></HTML>"

fileHtml.close()

Was wondering if i'm approaching this incorrectly and if there was another way to create HTML files and insert stored variable results

like image 779
user3927287 Avatar asked Aug 25 '14 15:08

user3927287


2 Answers

I think what you mean is called embedded Ruby or ERB for short. You can save your template as a separate file, for example template.erb. Then you can render the content in your ruby script with simple tags, for example <%= @var %>. You can pass the binding to ERB, that way you will be able to access instance variables (the ones with an @ at the beginning) inside your template.html.erb:

<HTML>
<HEAD>
  <!-- etc... -->
</HEAD>
<BODY>
  <DIV class='header'><img src='img.png' alt='logo'></div>
  <DIV class='secondSection'>
    <p><%= @date_time %></p>
    <p><%= @platform %></p>
    <p><%= @log %></p>
  </div>
  <DIV class='content'>
  <DIV class='div1'>
    <h2>Test Case 001: name</h2>
  </DIV>
  <DIV class='div2'>
    <p class='pass'><%= @pass %></p>
    <p class='fail'><%= @fail %></p>
  </DIV>
</BODY>
</HTML>

Then, in your program:

# do computation and set your instance variables
@date_time = ...
@platform  = ...
@log       = ...
@pass      = ...
@fail      = ...

# render template
template = File.read('./template.html.erb')
result = ERB.new(template).result(binding)

# write result to file
File.open('filename.html', 'w+') do |f|
  f.write result
end

# file is closed automatically with File.open do ... end

You can even do more advanced stuff like loops inside the template:

<% [1,2,3].each do |number| %>
  <%= number %>
<% end %>

notice the difference between <% ... %>, which will only run the code, and <%= ... %>, which will take the return value and "write" its string representation into the template.

like image 115
Patrick Oscity Avatar answered Oct 07 '22 16:10

Patrick Oscity


There is indeed - not one, but many. One simple one is to use heredoc string format:

string = <<-EOF
<HTML>
<HEAD>
... #{pass} ....
</HTML>
EOF

Another is using a templating engine. One is already baked into Ruby core library: ERb.

For even more abstraction, you could look into Haml and Sass gems, or Slim.

like image 34
Amadan Avatar answered Oct 07 '22 18:10

Amadan