0

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?

Katai
  • 2,773
  • 3
  • 31
  • 45
  • Assuming grunt-dust doesn't do anything too weird, you could just `require('dustjs-linkedin'); require('dustjs-helpers');` at the top of your gruntfile and that should register the helpers. – Interrobang Mar 03 '16 at 18:55
  • @Interrobang Thanks for the suggestion - I tried it, but sadly there's no change (and no error message either). It just compiles the templates again, completly ignoring all helper statements. – Katai Mar 07 '16 at 11:59
  • OK, I'll set it up and take a look. – Interrobang Mar 07 '16 at 18:24
  • I started setting it up, but grunt-dust seems to only **compile** templates, not render them? You don't need the helpers available to compile templates, only during rendering. How are you rendering the templates? – Interrobang Mar 10 '16 at 21:07
  • What I mean is that you don't need anything available during compiling. Compilation just transforms the template into its parsable form. It's only at runtime-- render-time-- that you need the helpers. That's when Dust will try to look for a helper and use it. – Interrobang Mar 11 '16 at 22:18

1 Answers1

1

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
Interrobang
  • 16,984
  • 3
  • 55
  • 63