Assemble: How can I generate pages from json/yaml?

纵饮孤独 提交于 2019-12-06 03:32:05

Since the options.pages feature has been implemented, you can add pages like this:

options: {
  pages: {
    "about": {
      "data": {
        "title": "About"
      },
      "content": "Content for {{title}}"
    },
    ...
  }
}

We aren't supporting automatic loading of a json/yml file, but you can do this inside your Gruntfile and add the object to options.pages that way...

module.exports = function(grunt) {

  grunt.initConfig({

    // package.json
    pkg: grunt.file.readJSON('package.json'),

    assemble: {
      options: {
        flatten: true,
        layoutdir: 'src/layouts',
        assets: 'dest/assets'
      },
      blog: {
        options: {
          engine: 'handlebars',
          layout: 'post.hbs',
          site: {
            title: 'This is my Blog'
          },
          pages: grunt.file.readJSON('pages.json')
        },
        files: { 'dest/': ['src/index.hbs'] }
      }
    }
  });

  // Load npm plugins to provide necessary tasks.
  grunt.loadNpmTasks('assemble');

  // Default task.
  grunt.registerTask('default', ['assemble']);

};

This example uses the post.hbs file as the layout for any pages defined in the pages.json file. It will also build a page for the index.hbs specified in the files src array. Right now, the files dest/src is required so Assemble knows where to write the files, but I think we'll add that to the options, or the page object so it can be run without the files object.

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