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

坚强是说给别人听的谎言 提交于 2019-12-24 02:01:50

问题


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


回答1:


Write something like this:

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


来源:https://stackoverflow.com/questions/16267122/how-to-include-a-static-json-file-for-compilation-with-jade-and-grunt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!