在grunt中将json数据转换为汇编模板 [英] getting json data into assemble templates in grunt

查看:132
本文介绍了在grunt中将json数据转换为汇编模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正通过grunt使用汇编( https://github.com/assemble/assemble )插件来构建静态页面。

我现在一直在使用grunt,所以了解它是如何工作的,但这是我第一次使用组合,所以我看着这个for ref( http://blog.parkji.co.uk/2013/07/06/building-a-static-site-using-grunt-and-assemble.html )。一切似乎都正常。



但是,我想介绍JSON数据,并查看了汇编网站上的文档( http://assemble.io/docs/Data.html )但是在我运行'grunt assemble'之后,它并没有渲染json data:(

我的gruntfile

 汇编:{
选项:{
layout:src / responsive / layouts / default.hbs,
data:'src / responsive / data / ** / *。json',
flatten:true
},
页面:{
文件:{
'src /':['src / responsive / pages / *。hbs']
}
}
},

测试json:

  {
name:This is a square widget,
modifier:widget-square
}

和文件夹结构:

   - 数据
- index.json
- 布局
- default.hbs
- 页面
- index.hbs

在网页/ index.hbs中,我尝试调用{{index.name}}或简单地{{name}}(我尝试过)我没有发现任何错误(事实上,如果我让json无效的抱怨,所以它正在读取它)。 / p>

在我疯狂前,任何帮助都很值得赞赏....
$ b $ p感谢,
Adrian

p>

解决方案

您的JSON在这里是问题所在。您的中有额外的空格。您的JSON格式应如下所示:

  {
name:这是一个方形小工具,
modifier:widget-square
}


I am using assemble (https://github.com/assemble/assemble) via a grunt plugin to build static pages.

I have been using grunt for a while now so understand how it all works, yet this is my first time using assemble so I looked at this for ref (http://blog.parkji.co.uk/2013/07/06/building-a-static-site-using-grunt-and-assemble.html). Everything seemed to be working fine.

However, I want to introduce JSON data to the mix and have looked at the docs on the assemble site (http://assemble.io/docs/Data.html) yet after I run 'grunt assemble' it doesnt render the json data :(

My gruntfile

 assemble: {
        options: {
            layout: "src/responsive/layouts/default.hbs",
            data: 'src/responsive/data/**/*.json',
            flatten: true
        },
        pages: {
            files: {
                'src/': ['src/responsive/pages/*.hbs']
            }
        }
    },

test json:

{
  "name ": "This is a square widget" ,
  "modifier ": "widget-square" 
 }

and folder structure:

- data
-- index.json
- layouts
-- default.hbs
- pages
-- index.hbs

Within pages/index.hbs I am trying to call {{ index.name }} or simply {{ name }} (I have tried both) to no avail.

Im pulling my hair out as grunt is giving no errors (and in fact if I make json not valid grunt complains so it is reading it).

Any help much appreciated before I go crazy....

Thanks, Adrian

解决方案

Your JSON here is the issue. You had extra spaces in your keys. Your JSON should be formatted as the following:

{
  "name": "This is a square widget",
  "modifier": "widget-square" 
}

这篇关于在grunt中将json数据转换为汇编模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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