-->

getting json data into assemble templates in grunt

2019-06-27 13:53发布

问题:

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

回答1:

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