Skip to content

How to use the options.baseDir #2

@Icehunter

Description

@Icehunter

I have the following setup with hapij 8.4.0:

        function (cb) {
            var caching = (process.env.NODE_ENV === 'production') ? true : false;
            var engine = require('hapi-dust');

            server.views({
                defaultExtension: 'dust',
                engines: {
                    dust: engine
                },
                isCached: caching,
                layoutPath: path.join(__dirname, './views/layouts'),
                partialsPath: path.join(__dirname, './views/partials'),
                path: path.join(__dirname, './views/pages'),
                relativeTo: __dirname
            });
            cb();
        },

No matter what I usually try I'm unable to specify layout files dynamically as I get a ENOENT errors.

My current layout file is:

<html>

<body>
    <div id="content">{+content/}</div>
</body>

</html>

If I set layout to true with hapijs view options then the layout renders with no issue but splash page does not load.

I've modified the signature of my internal use of hapi dust to something like this:

var dust;
var baseDir;
var fs = require('fs');
var Path = require('path');

try {
    dust = require('dustjs-linkedin');
    try {
        require('dustjs-helpers');
    }
    catch (ex) {}
}
catch (ex) {
    try {
        dust = require('dust');
    }
    catch (ex) {}
}

if (!dust) {
    throw new Error('"dustjs-linkedin" or "dust" module not found');
}

var getBaseDir = function (options) {
    if (options && options.baseDir) {
        return options.baseDir;
    }
    return baseDir;
};

var getTemplateExt = function (options) {
    if (options && options.defaultExt && options.defaultExt.length > 0) {
        return '.' + options.defaultExt;
    }
    return '.dust';
};

module.exports = function (engineOptions) {
    engineOptions = engineOptions || {};
    baseDir = engineOptions.baseDir || '';
    return {
        module: {
            compile: function (template, options, callback) {
                console.log(arguments);
                dust.onLoad = function (name, callback) {
                    var templateFile = Path.join(getBaseDir(options), name + getTemplateExt(options));
                    fs.readFile(templateFile, function (err, data) {
                        if (err) {
                            throw err;
                        }
                        callback(err, data.toString());
                    });
                };
                var compiled = dust.compileFn(template, options && options.name);
                process.nextTick(function () {
                    callback(null, function (context, options, callback) {
                        console.log(arguments);
                        compiled(context, callback);
                    });
                });
            }
        },
        compileMode: 'async'
    };
};

Instantiate it like so:

            var engine = require('hapi-dust')({
                baseDir: path.join(__dirname, './views')
            });

I can now do the following in a dust file:

{>"layouts/layout" /}
{<content}

<div><h1>Hello DUST</h1></div>

{/content}

Is there a way or example out there on how-to use multiple layouts and this plugin rather than changing the code?

Thanks for the great work!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions