Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including metalsmith-models with all npm packages installed. Try it out:

var metalsmithModels = require("metalsmith-models")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

metalsmith-models v0.0.3

Metalsmith plugin to include JSON models into files and expose the data as metadata on the file object.

metalsmith-models

Metalsmith plugin to include JSON models into files and expose the data as metadata on the file object.

Installation

npm install --save metalsmith-models

CLI

In metalsmith.json

{
    "source": "src",
    "destination": "build",
    "plugins": {
        "metalsmith-models": {
            "directory": "models"
        },
    }
}

API

var Metalsmith = require('metalsmith');
var models = require('metalsmith-models');

var metalsmith = new Metalsmith(__dirname)
.use(models({
  directory: "models"
}));

Options

options.directory

Type: String Default value: models Source directory of all JSON files.

Usage

JSON file can be loaded into the files using YAML front matter as below:

data_file.json
{
  "key1": "value 1",
  "key2": "value 2"
}
page.hbs
---
model: data_file
---

Where data_file is the name of the JSON file that is placed under the models directory (See CLI Usage or API section).

The path of the JSON files is always relative to the root directory (Ex: models).

How to use the data.

In case of pages using handlebars:

{{model.key1}}

Loading multile JSON files

If you need to load multiple JSON files in a page, use below format:

---
model:
  obj1: data_file1
  obj2: data_file2
---
How to use the data.

In case of pages using handlebars:

{{model.obj1.key1}}
{{model.obj2.key1}}

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free