2017-10-08 67 views
1

我已經寫在YAML,我想用ERB中間人來呈現文件。我該如何渲染yaml文件才能轉換爲HTML。我後來想用CSS來設計它。我沒有發現如何在任何地方在線執行此基本任務的信息。如何獲得YAML文件以中間人渲染ERB

例如說我有YAML文件:

main_title: 'Ruby Operators' 

sections: 
    - section_title: 'Arithmetic' 
    items: 
     - title: '[ + ] Addition: ' 
     value: 'Adds values on either side of the operator' 
     - title: '[ − ] Subtraction: ' 
     value: 'Subtracts right hand operand from left hand operand' 
     - title: '[ * ] Multiplication: ' 
     value: 'Multiplies values on either side of the operator.' 

    - section_title: 'Comparison' 
    items: 
     - title: '[ == ]' 
     value: 'Checks if the value of two operands are equal or not, if yes then condition becomes true.' 
     - title: '[ != ]' 
     value: 'Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.' 

我想呈現像這樣在HTML

<!DOCTYPE html> 
<html> 

<head> 
    <title>Beautifyconverter.com Yaml To HTML Converter</title> 
</head> 

<body> 
    <table> 
     <tr> 
      <td>main_title</td> 
      <td>sections.0.section_title</td> 
      <td>sections.0.items.0.title</td> 
      <td>sections.0.items.0.value</td> 
      <td>sections.0.items.1.title</td> 
      <td>sections.0.items.1.value</td> 
      <td>sections.0.items.2.title</td> 
      <td>sections.0.items.2.value</td> 
      <td>sections.1.section_title</td> 
      <td>sections.1.items.0.title</td> 
      <td>sections.1.items.0.value</td> 
      <td>sections.1.items.1.title</td> 
      <td>sections.1.items.1.value</td> 
     </tr> 
     <tr> 
      <td>Ruby Operators</td> 
      <td>Arithmetic</td> 
      <td>[ + ] Addition: </td> 
      <td>Adds values on either side of the operator</td> 
      <td>[ − ] Subtraction: </td> 
      <td>Subtracts right hand operand from left hand operand</td> 
      <td>[ * ] Multiplication: </td> 
      <td>Multiplies values on either side of the operator.</td> 
      <td>Comparison</td> 
      <td>[ == ]</td> 
      <td>"Checks if the value of two operands are equal or not</td> 
      <td> if yes then condition becomes true."</td> 
      <td>[ != ]</td> 
      <td>"Checks if the value of two operands are equal or not</td> 
      <td> if values are not equal then condition becomes true."</td> 
     </tr> 
     <tr> 
      <td></td> 
     </tr> 
    </table> 
</body> 

</html> 
+0

如果這將會是有益的可以給你更多的細節,如你想要表看起來,例如它的單元格數量是否有固定的寬度或高度?列和行應該代表什麼? –

回答

0

我會嚴格限制這個答案從YAML訪問數據文件。這將讓你的數據輸出你後,你可以決定如何從那裏樣式。

假設你的文件被命名爲mydata.yaml和位於你的中間人應用程序的文件夾/data,下面的代碼將努力產生嵌套循環數據你正在尋找:

<h1><%= data.mydata.main_title %></h1> 

<% data.mydata.sections.each do |section| %> 
    <h2><%= section.section_title %><h2> 
    <% section.items.each do |item| %> 
     <h3><%= item.title %></h3> 
     <h4><%= item.value %></h4> 
    <% end %> 
<% end %>