【问题标题】:How to get YAML files to render in middleman with erb如何使用 erb 让 YAML 文件在中间人中呈现
【发布时间】:2018-03-20 01:04:05
【问题描述】:

我有一个用 yaml 编写的文件,我想通过中间人使用 erb 进行渲染。如何渲染它以便将 yaml 文件转换为 HTML。稍后我想使用 CSS 对其进行样式设置。我在任何地方都找不到有关如何在线完成此基本任务的信息。

例如说我有 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>

【问题讨论】:

  • 如果您能提供更多关于您希望表格外观的详细信息会很有帮助,例如就单元格数量而言,它是否具有固定的宽度或高度?列和行应该代表什么?

标签: ruby yaml erb middleman


【解决方案1】:

我将这个答案严格限制为从 YAML 文件访问数据。这将为您提供所需的数据输出,您可以从那里决定如何设置它的样式。

假设您的文件名为 mydata.yaml 并位于 Middleman 应用程序的 /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 %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多