robkuz
robkuz

Reputation: 9924

How to include a static JSON file for compilation with Jade and Grunt

how do I compile Jade templates to static HTML via Grunt where my data is already held in a JSON file?

Say I have this Gruntfile.js

module.exports = function(grunt) {

    grunt.loadNpmTasks('grunt-contrib-jade');

    grunt.initConfig(
    {
        jade: {
            html: {
                src: './*.jade',
                dest: './index2.html',
                options: {
                    client: false,
                    pretty: true
                }
            }
        }
    });

    grunt.registerTask('default', 'jade');
};

This JSON file (./data.json)

{
    "foo": {value: 1},
    "bar": {value: 2},
    "baz": {value: 3}
}

And this Jade (./index.jade)

ul
    li data_loaded_from_json.foo.value
    li data_loaded_from_json.bar.value
    li data_loaded_from_json.baz.value

So how can I teach grunt to load the json file and make it available to Jade as a global variable?

Thanks for your help

Upvotes: 2

Views: 5119

Answers (1)

i11v
i11v

Reputation: 62

Write something like this:

 jade: {
    html: {
        src: './*.jade',
        dest: './index2.html',
        options: {
            client: false,
            pretty: true,
            data: grunt.file.readJSON("data.json")
        }
    }
}

Upvotes: 4

Related Questions