Skip to content

Instantly share code, notes, and snippets.

@jonschlinkert
Created August 25, 2013 22:49
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonschlinkert/6336762 to your computer and use it in GitHub Desktop.
Save jonschlinkert/6336762 to your computer and use it in GitHub Desktop.
Different Gruntfile configurations for Assemble.
module.exports = function(grunt) {
  // Project configuration.
  grunt.initConfig({
    // Build HTML from templates and data
    assemble: {
      options: {
        flatten: true,
        assets: 'dist/assets',
        partials: ['templates/includes/*.hbs'],
        helpers: ['templates/helpers/helper-*.js'],
        layout: 'templates/layouts/default.hbs',
        data: ['templates/data/*.{json,yml}']
      },
      pages: {
        src: 'templates/*.hbs',
        dest: 'dist/'
      }
    }
  });

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

  // Default tasks to be run.
  grunt.registerTask('default', ['assemble']);
};

Gruntfile.js

module.exports = function(grunt) {
  // Add this line to the Gruntfile before the `grunt.initConfig` object
  grunt.util._.mixin(require('./src/helpers/mixins.js').init(grunt));

  // Project configuration.
  grunt.initConfig({
    // tasks
  )};
};

then add this to ./src/helpers/mixins.js or whatever file you want:

exports.init = function(grunt) {
  var exports = {};
  grunt.util._.mixin({
    /**
     * Slugify a string. Makes lowercase, and converts dots and spaces to dashes.
     * This is NOT a 'real' slugifier, it's just an example.
     * @param  {String} urlString [the string you want to slugify]
     * @return {String}           [a slugified string]
     */
    slugify: function(urlString) {
      return urlString.replace(/ /g, '-').replace(/\./, '-').toLowerCase();
    }
  });
  return exports;
};

Now the mixin can be used anywhere in your project. For example, in a template (page) we can do something like this:

---
path: ONE Two three
slug: <%= _.slugify(path) %>
---

<strong>{{slug}}</strong>

returns:

<strong>one-two-three</strong>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment