【问题标题】:How to include a static JSON file for compilation with Jade and Grunt如何包含静态 JSON 文件以使用 Jade 和 Grunt 进行编译
【发布时间】:2013-04-28 19:51:33
【问题描述】:

如何通过 Grunt 将 Jade 模板编译为静态 HTML,其中我的数据已经存在 保存在 JSON 文件中?

假设我有这个 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');
};

此 JSON 文件 (./data.json)

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

还有这个翡翠(./index.jade)

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

那么我怎样才能教 grunt 加载 json 文件并使其可供 Jade 使用 作为全局变量?

感谢您的帮助

【问题讨论】:

    标签: json pug gruntjs


    【解决方案1】:

    这样写:

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

    【讨论】:

      猜你喜欢
      • 2013-07-21
      • 2016-09-04
      • 1970-01-01
      • 2016-03-13
      • 2016-08-31
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 2014-11-02
      相关资源
      最近更新 更多