prosenjit-itobuz
9/3/2015 - 4:15 AM

Couple useful heplers for ghost

Couple useful heplers for ghost

var hbs = require('express-hbs'),
    api = require('core/server/api'),
    _ = require('lodash'),
    async = require('express-hbs/lib/async'), // To redefine `registerAsyncHelper`
    registerAsyncHelper;

// Redefine `registerAsyncHelper` from `express-hbs`
registerAsyncHelper = function (name, fn) {
    hbs.handlebars.registerHelper(name, function (context, options) {
        // Pass `[context, options]` as arg instead of `context` only
        return async.resolve(fn.bind(this), [context, options]);
    });
};

module.exports = function () {

    // {{#node_env}} Helper
    //
    // Example:
    // {{#node_env production}}
    //     ...production only 
    // {{/node_env}}
    //
    hbs.registerHelper('node_env', function (env, options) {
        return (options.data.root.settings.env === env) ? options.fn(this) : options.inverse(this)
    });


    // {{#by_tag}} Helper
    //
    // Example:
    // {{#by_tag 'dev'}}
    //    {{#foreach posts}}
    //        {{title}}
    //        {{content}}
    //    {{/foreach}}
    // {{/by_tag}}
    //
    // TODO `page` or smth like this functionality
    //
    registerAsyncHelper('by_tag', function (context_data, callback) {
        var context = context_data[0], // get context and options passed from context_data array
            options = context_data[1],
            parameters = (options || {}).hash || {},
            request = {
                context: {internal: true},
                tag: context
            };

        if (parameters.hasOwnProperty('limit')) {
            request.limit = parameters.limit
        }

        return api.posts.browse(request).then(function (responce) {
            var data;
            if (options !== undefined && typeof options.fn === 'function') {
                data = hbs.handlebars.createFrame(options.data || {});
                data.posts = responce.posts;
                console.log(data);
                callback(options.fn(data))
            } else {
                callback('')
            }
        });
    });

    
    // {{{hello_kitty}}} Sample Helper
    hbs.registerHelper('hello_kitty', function () {
        return new hbs.handlebars.SafeString('Hello =^oo^= Kitty!');
    });
    
};