Gruntjs grunt从一个数据文件中汇编多个文件

Gruntjs grunt从一个数据文件中汇编多个文件,gruntjs,assemble,grunt-assemble,Gruntjs,Assemble,Grunt Assemble,我正在尝试使用一个模板和一个数据文件组合多个文件 data.json { "site": { "title": "A Blog", "author": "Jon Schlinkert" }, "pages": [ { "metadata": { "title": "Blog Post #1", "summary": "", "categories": [""], "layout": "

我正在尝试使用一个模板和一个数据文件组合多个文件

data.json

{
  "site": {
    "title": "A Blog",
    "author": "Jon Schlinkert"
  },
  "pages": [
    {
      "metadata": {
        "title": "Blog Post #1",
        "summary": "",
        "categories": [""],
        "layout": "post.hbs",
        "gists": ["5898072"]
      },
      "content": "This would get passed into the `body` tag, but it's not necessary if you only need to add a post from a gist."
    },
    {
      "metadata": {
        "title": "Blog Post #2",
        "summary": "",
        "categories": [""],
        "layout": "post.hbs",
        "gists": ["5898077", "5898078"]
      }
    },
    {
      "metadata": {
        "title": "Blog Post #3",
        "summary": "",
        "categories": [""],
        "layout": "post.hbs",
        "gists": ["5909393"]
      }
    }
  ]
}
{
    "pages": [
        {
            "filename": "post1",
            "data": {
                "title": "Blog Post #1",
                "gists": ["5898072"]
            },
            "content": "This would get passed into the `body` tag, but it's not necessary if you only need to add a post from a gist."
        },
        {
            "filename": "post2",
            "data": {
                "title": "Blog Post #2",
                "gists": ["5898077", "5898078"]
            }
        },
        {
            "filename": "post3",
            "data": {
                "title": "Blog Post #3",
                "gists": ["5909393"]
            }
        }
    ]
}
模板.hbs

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>{{site.title}}</title>
    <link href="http://libs.github.io/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <meta name="author" content="{{site.author}}">
  </head>
  <body>
    {{#each gists}}
      {{gist this}}
    {{/each}}
    {{> body }}
    <script src="http://libs.github.io/bootstrap/css/bootstrap.min.js"></script>
  </body>
</html>
这个问题有着诱人的线索,但我不知道如何适应:

我相信这个功能是在这个问题之后创建的
我终于明白了:

Gruntfile.js

module.exports = function(grunt) {  

    // Project configuration.
    grunt.initConfig({
        data : grunt.file.readJSON('src/data/data.json'),
        assemble: {
            inline_pages: {
                options: {
                    layout: "./src/templates/post.hbs",
                    site: {
                        title: "A Blog",
                        author: "Jon Schlinkert"
                    },
                    pages: '<%= data.pages %>'
                },
                files: {
                  'dist/': ['!*']
                }
            }
        },
        clean: {
            options: { force: true },
            all: ['./dist/*.html']
        }
    });

    grunt.loadNpmTasks('grunt-assemble');
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.registerTask('default', ['clean', 'assemble']);
};
博士后

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>{{site.title}}</title>
    <link href="http://libs.github.io/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <meta name="author" content="{{site.author}}">
  </head>
  <body>

    The page title is {{{this.title}}}
    The gist number is {{{this.gists}}}

    This is the body:
    {{> body}}
    <script src="http://libs.github.io/bootstrap/css/bootstrap.min.js"></script>
  </body>
</html>

{{site.title}
页面标题为{{{this.title}}}
要点编号为{{{this.gists}}}
这是正文:
{{>body}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>{{site.title}}</title>
    <link href="http://libs.github.io/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <meta name="author" content="{{site.author}}">
  </head>
  <body>

    The page title is {{{this.title}}}
    The gist number is {{{this.gists}}}

    This is the body:
    {{> body}}
    <script src="http://libs.github.io/bootstrap/css/bootstrap.min.js"></script>
  </body>
</html>