将一段html文本与ruby变量链接到html文件

时间:2018-03-23 10:58:26

标签: html ruby

我有一段包含一些ruby变量的html文本,并且依赖于迭代次数未知的循环。我现在(和工作):

test.rb (件)

head = <<-HEAD
  <html>
    <head>
      <title>Title</title>
      <link rel="stylesheet" type="text/css" href="style.css">
      <script src="scripts.js" type="text/javascript"></script>
    </head>
    <body>
  HEAD

body = '<div class="flex-container">'

items.each do |i|
  body << %(
    <div class="container">
      <button id='action#{i.ref}'
        style="background-color: 'inherit';"
        onmouseover="this.id='action'; this.style.background='highlight';"
        onclick="selectItem()"
        onmouseout="this.id='action#{i.ref}'; this.style.background='inherit';"
        value="#{i.root}">
      <img src="#{i.image}" alt="#{i.name}_img">
      <div>#{i.name}</div>
      </button>
    </div>
  )
end

body += '</div></body></html>'
html = head + body

这就是我生成html文本的方式。我想要一个单独的html文件,并以某种方式将正文中的内容链接到上一个示例的ruby代码,即:

的test.html

<html>
  <head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="scripts.js" type="text/javascript"></script>
  </head>
  <body>
    <div class="flex-container">
       <!-- HERE HAVE SOME LINK TO THE BODY OF test.rb -->
    </div>
  </body>
</html>

任何提示?感谢。

1 个答案:

答案 0 :(得分:1)

您的文件test.html应该有扩展&#39; html.erb &#39;

<强> test.html.erb

public void onClick(View view) {

        for (int x = 0; x < scoreList.size()&& x < totalList.size(); x++)

        {
            CalculateActivity.this.gradesList.add(scoreList.get(x) / totalList.get(x));

        }



        for (int i = 0; i <  CalculateActivity.this.gradesList.size(); i++) {
             int crdtHrs = ((Course) CalculateActivity.this.coursesList.get(i)).getCreditHours();

                totalCrdtHrs += (int) crdtHrs;
                averageGrade1 += (((int) CalculateActivity.this.gradesList.get(i)) * (((Course) CalculateActivity.this.coursesList.get(i)).getCreditHours()));
                averageGrade2 = averageGrade1/totalCrdtHrs;



        }

        int gpaCalc = averageGrade2;


        CalculateActivity.this.gpa.setText(String.format(""+gpaCalc));
    }
}

test.rb 文件中,您应该以这种方式声明实例变量:

<html>
  <head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="scripts.js" type="text/javascript"></script>
  </head>
  <body>
    <div class="flex-container">
       <%= @body %>
    </div>
  </body>
</html>