Katai
Katai

Reputation: 2937

How to use dustjs-helpers with grunt-dust?

I'm using grunt-dust to compile dustjs templates, but now I've run into the problem that I need to use dust helpers (like @eq) which apparently grunt-dust ignores completly.

I've installed dustjs-helpers over npm but just can't figure out how to adjust my grunt configuration to handle them. I simplified it to keep the relevant parts.

grunt.initConfig( {
    ...

    dust: {
        defaults: {
            files: {
                'public/js/views.js': [ ... directories ... ]
            },
            options: {
                wrapper:  false,
                basePath: 'private/',
                useBaseName: true,
                wrapperOptions: {
                    templatesNamesGenerator: function( options, file ) {
                        // returns an altered template name
                    }
                }
            }
        }
    },

    ...
} )

...

grunt.loadNpmTasks('grunt-dust')

...

grunt.registerTask( ... )

So far, it works fine and compiles the dustjs templates as expected.

How can I include dustjs-helpers with grunt-dust?

Upvotes: 0

Views: 173

Answers (1)

Interrobang
Interrobang

Reputation: 17434

You do not need the helpers available when grunt-dust compiles the templates. Compiling is the process of turning the template into a Dust function, and the helpers won't actually be invoked.

When you do need dustjs-helpers available is during rendering. So however you are rendering your templates, you'll want to make sure that the helpers are attached to the dust instance you're using to render. You do that simply by requiring them:

let dust = require('dustjs-linkedin');
require('dustjs-helpers'); // helpers autoattach to the `dust` object

dust.render(template, context); // this template will be able to use helpers

Upvotes: 1

Related Questions