1.1.1 • Published 3 years ago

@dukai.net/hapi-dust v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

hapi-dust

Dust view engine toolkit support for Hapi.

Installation:

npm install @dukai.net/hapi-dust

or:

yarn add @dukai.net/hapi-dust

Use:

// Load all views 
server.loadDustViews('<path to my compiled views>');
// Loader method for dust
const myLoaderMethod = (template, callback) => {
    try {
        template = template.toLowerCase().includes('.dust') ? template : (template + '.dust');
        const templateFile = path.join(myPathToView, template);
        let content = readFileSync(templateFile, 'utf8');
        if (typeof callback === 'function') {
            callback(null, content);
        }
    } catch (err) {
        if (typeof callback === 'function') {
            callback(err, null);  
        } 
    }
};

// One by one loading of templates via event. 
// When dust given a task to render, it will call this method to load it from file.
server.onLoadDustTemplate(myLoaderMethod);
// Update dust configuration
server.updateDustConfig({ whitespace: true });
const dust = server.getDust();
// Do what you want with the object
dust.config.whitespace = true;
dust.onLoad = myLoaderMethod;
const model = {
    alpha: 'abc',
    beta: 123
};
// Add default properties to every model
// 1. static data
server.modelDefaults({ debug: true, somethingElse: 1234 });
// When used in renderDustView method, the model will look like: 
// {
//   alpha: 'abc',
//   beta: 123,
//   debug: true,
//   somethingElse: 1234
// }

// 2. method
server.modelDefaults((model) => {
    // inject whatever extra property you want
    if (model.alpha === 'abc') {
    	model.myOwnFlag = true;
    }
});
// Render method available in the server:
server.renderDustView('myDustTemplate.dust', model);
// or in the toolkit: 
server.route([
    {
        path: '/apple/loadstuff',
        method: 'GET',
        handler: async (request, h) => {
            const dal = h.dal(request); // See more about this in the @dukai.net/hapi-settings package
            const model = await dal.apple.getStuff();
            return h.renderDustView('myDustTemplate.dust', model);
        },
        options: {...}
    }
]);
1.1.1

3 years ago

1.1.0

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago