如何在Jade和Grunt中包含静态JSON文件进行编译 [英] How to include a static JSON file for compilation with Jade and Grunt

查看:105
本文介绍了如何在Jade和Grunt中包含静态JSON文件进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

说我有这个Gruntfile。 js

  module.exports = function(grunt){

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

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}
}

而这个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

那么我该如何教grunt来加载json文件并将其提供给Jade
作为全局变量?



感谢您的帮助

解决方案

写下这样的内容:

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


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

解决方案

Write something like this:

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

这篇关于如何在Jade和Grunt中包含静态JSON文件进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆