Search code examples
gruntjshandlebars.jsassemble

Assemble handlebar template relative to its path


So I'm trying to look through a directory and compile all hbs files. But the thing is that the hbs file should compile in the folder it lies in.

pseudo code:

assemble: {
            options: {
                flatten: false,
                assets: 'assets',
                dev: true,
                livereloadPort: '<%- livereloadPort %>',
                partials: ['dev/assemble/partials/*.hbs'],
                layout: 'dev/assemble/banner.hbs',
                data: ['dev/assemble/data/*.{json,yml}']
            },

            // ----------------------------------------------------------------
            // assembles

            dev: {
                options: {
                    data: ['dev/assemble/data/*.{json,yml}']
                },
                src: ['dev/banners/**/*.hbs'],
                expand: true,
                dest: '<% SAME AS SRC FILE %>',
                ext: '.html'
            }
        },

So basically this folder structure;

/banners/
    /bannerfolder_1/
        handlebarfile.hbs
    /bannerfolder_2/
        another_handlebarfile.hbs

Would result in this;

/banners/
    /bannerfolder_1/
        handlebarfile.hbs
        handlebarfile.html
    /bannerfolder_2/
        another_handlebarfile.hbs
        another_handlebarfile.html

Can this be done?


Solution

  • Try the following globbing pattern / dynamic mapping

    files: {
              expand: true,     // Enable dynamic expansion.
              cwd: 'dev/',      // Src matches are relative to this path.
              src: ['banners/**/*.hbs'], // Actual pattern(s) to match.
              dest: 'dev/',   // Destination path prefix.
              ext: '.html',   // Dest filepaths will have this extension.
              extDot: 'last'   // Extensions in filenames begin after the last dot
            }