mikefrey / hapi-dust

hapi compatibility for dust.js
MIT License
5 stars 3 forks source link

How to use the options.baseDir #2

Open Icehunter opened 9 years ago

Icehunter commented 9 years ago

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!

mikefrey commented 9 years ago

@Icehunter I've never tried using multiple layouts. Can you put your changes into a Pull Request so I can see the difference and test more easily? Thanks!

Icehunter commented 9 years ago

I definitely can. Give me a few days, still recovering from adult onset chicken pox...

danactive commented 7 years ago

@Icehunter How did you resolve your issue?