且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在grunt中将json数据转换为汇编模板

更新时间:2023-02-11 07:46:31

您的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" 
}