如何获得YAML文件以中间人渲染ERB
问题描述:
我已经写在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>
答
我会严格限制这个答案从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 %>
如果这将会是有益的可以给你更多的细节,如你想要表看起来,例如它的单元格数量是否有固定的宽度或高度?列和行应该代表什么? –