0

I have a json data file with multiple objects with named keys in it.

{
  "berlin:" : {
    "location": "Berlin",
    "folder": "berlin-2016"
  },
  "seattle" : {
    "location": "Seattle ",
    "folder": "seattle-2016"
  }
}

In my content file I would like to specify which object in the model to use and then refer to that in swig. Something like this:

---
model:
  conference: conferences['berlin']
---    

{{ model.conference.location }}

Is this possible?

Flexo
  • 87,323
  • 22
  • 191
  • 272

1 Answers1

0

That's definitely possible with metalsmith. I don't have a complete picture of your build process, but for this solution you'll have to use the metalsmith javascript api:

./data.json

{
  "berlin:" : {
    "location": "Berlin",
    "folder": "berlin-2016"
  },
  "seattle" : {
    "location": "Seattle ",
    "folder": "seattle-2016"
  }
}

./build.js

// Dependencies
var metalsmith = require('metalsmith');
var layouts = require('metalsmith-layouts');

// Import metadata
var metadata = require('./data.json');

// Build
metalsmith(__dirname)
  // Make data available
  .metadata(data)

  // Process templates
  .use(layouts('swig'))

  // Build site
  .build(function(err){
    if (err) throw err;
  });

Then run node build.js in your root project folder to build. In your templates the data from data.json would then be available as {{ berlin.location }}.

You can also do this without the javascript api (which I don't recommend because you lose some flexibility), in which case you would use a plugin (for example: metalsmith-json)