如何使用erb使YAML文件在中间人中呈现 [英] How to get YAML files to render in middleman with erb

查看:82
本文介绍了如何使用erb使YAML文件在中间人中呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用yaml编写的文件,我想使用erb与中间人一起渲染。我该如何渲染,以便将yaml文件转换为HTML。我稍后想使用CSS设置样式。我找不到任何地方如何在线执行此基本任务的信息。

I have a file written in yaml that I would like to render with middleman using erb. How do I render this so that the yaml file will be translated to HTML. I would later like to style it using CSS. I have found no information of how to do this basic task online anywhere.

例如说我有yaml文件:

For example say I have the yaml file:

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中渲染这样的东西

I would like to render something like this in 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文件。这将为您提供所需的数据输出,并且您可以决定如何从那里设置样式。

I'll limit this answer strictly to accessing data from the YAML file. This will get you the data output you are after, and you can decide how to style it from there.

假设您的文件名为 mydata.yaml 并位于 / data 文件夹中,以下代码将生成您要查找的嵌套数据循环:

Assuming your file is named mydata.yaml and located in the /data folder of your Middleman app, the following code will work to generate the nested data loops you are looking for:

<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 %>

这篇关于如何使用erb使YAML文件在中间人中呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆