Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用ruby创建HTML文件_Html_Ruby - Fatal编程技术网

用ruby创建HTML文件

用ruby创建HTML文件,html,ruby,Html,Ruby,我目前正在用ruby创建一个HTML格式的报告页面 例如: 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

我目前正在用ruby创建一个HTML格式的报告页面

例如:

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()
fileHtml=File.new(“filename.html”,“w+”)
fileHtml.puts“”
fileHtml.puts“”
fileHtml.puts“”
fileHtml.puts“正文{字体系列:Helvetica Neue,无衬线;字体大小:18px;颜色:#525252;填充:0;边距:0;背景:#f2f2f2;}”
fileHtml.puts“.content{最大宽度:1180px;填充:40px;}”
fileHtml.puts“.div1{页边距顶部:28px;页边距底部:1px;背景色:#fff;填充:10px 40px;填充底部:8px;}”
fileHtml.puts“.div2{页边距顶部:2px;高度:25%;页边距底部:28px;背景色:#fff;填充:10px 40px;填充底部:8px;}”
fileHtml.puts“.header{背景色:白色;高度:16%;最小高度:110px;位置:相对;宽度:100%;-webkit用户选择:无;}”
fileHtml.puts“.secondSection{背景色:#e8e8e8;高度:16%;最小高度:110px;位置:相对;宽度:100%;-webkit用户选择:无;}”
通过{color:#ffffff;背景:#34d9a2;填充:10px 20px 10px 20px;文本装饰:无;宽度:50px;}”
fileHtml.puts“.fail{color:#ffffff;背景:#f25e6a;填充:10px 20px 10px 20px;文本装饰:无;宽度:50px;}”
fileHtml.puts“”
fileHtml.puts“”
fileHtml.puts“”
fileHtml.puts“”
fileHtml.put“{dateTime}

{platform}

{log}

” fileHtml.puts“” fileHtml.puts“测试用例001:name” fileHtml.put“

{pass}

{fail}

” fileHtml.puts“” fileHtml.puts“” fileHtml.close()

我想知道我是否处理得不正确,是否有其他方法来创建HTML文件并插入存储的变量结果

使用herdoc怎么样

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

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

fileHtml.close()
fileHtml=File.new(“filename.html”,“w+”)

fileHtml.write确实有——不是一个,而是很多。一个简单的方法是使用heredoc字符串格式:

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

string=我想你的意思是调用。您可以将模板另存为单独的文件,例如
template.erb
。然后,您可以使用简单的标记在ruby脚本中呈现内容,例如
。您可以将
绑定
传递给ERB,这样您就可以访问
模板.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>
您甚至可以在模板中执行更高级的操作,如循环:

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

请注意
(只运行代码)和
(将返回值“写入”其字符串表示形式到模板中)之间的区别。

请仔细阅读以下内容,希望这会有所帮助。
<% [1,2,3].each do |number| %>
  <%= number %>
<% end %>