Search code examples
javascriptgruntjsassemble

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


Solution

  • 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.