简体   繁体   中英

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

Is there a way to generate pages from json/yaml if you provide a layout? I thought this was possible, but can't find in docs.

This is currently being tracked here in GitHub: http://webb.li/QjTX

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM