Skip to content

Instantly share code, notes, and snippets.

@henrytao-me
Created December 31, 2015 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrytao-me/a4da55236fc9fb988f53 to your computer and use it in GitHub Desktop.
Save henrytao-me/a4da55236fc9fb988f53 to your computer and use it in GitHub Desktop.
ByTag helper for ghost blogging platform
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() {
// {{#by_tag}} Helper
//
// Example:
// {{#by_tag 'dev,prod' limit=3}}
// {{#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
}
};
var tags = (context || '').split(',');
_.each(tags, function(tag, i) {
tags[i] = 'tag:' + tags[i];
});
if (tags.length > 0) {
request.filter = tags.join(',');
}
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;
data.pagination = {
page: 1,
prev: 0,
next: 0,
pages: 1,
total: request.limit || 100,
limit: request.limit || 100
};
callback(options.fn(data))
} else {
callback('')
}
});
});
};
@henrytao-me
Copy link
Author

Tested with Ghost 0.7.4

Installation

  • Create helper.js in ghost directory (code above)
  • Add first line to your config.js: require('./helper')();
  • Restart ghost

Usage

Select posts by tag. Optional limit parameter.

Example:

 {{#by_tag 'dev'}}
     {{#foreach posts}}
         {{title}}
         {{content}}
     {{/foreach}}
 {{/by_tag}}

 {{#by_tag 'music,sport' limit=3}}
     {{#foreach posts}}
         {{title}}
         {{content}}
     {{/foreach}}
 {{/by_tag}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment