2013-11-28 06:45:01 +04:00
|
|
|
var templates = {},
|
|
|
|
hbs = require('express-hbs'),
|
2014-05-09 14:11:29 +04:00
|
|
|
errors = require('../errors');
|
2013-11-28 06:45:01 +04:00
|
|
|
|
|
|
|
// ## Template utils
|
|
|
|
|
2013-12-02 03:31:55 +04:00
|
|
|
// Execute a template helper
|
|
|
|
// All template helpers are register as partial view.
|
2015-03-28 19:00:57 +03:00
|
|
|
templates.execute = function (name, context, options) {
|
2013-12-02 03:31:55 +04:00
|
|
|
var partial = hbs.handlebars.partials[name];
|
2013-11-28 06:45:01 +04:00
|
|
|
|
2013-12-02 03:31:55 +04:00
|
|
|
if (partial === undefined) {
|
|
|
|
errors.logAndThrowError('Template ' + name + ' not found.');
|
|
|
|
return;
|
|
|
|
}
|
2013-11-28 06:45:01 +04:00
|
|
|
|
2013-12-02 03:31:55 +04:00
|
|
|
// If the partial view is not compiled, it compiles and saves in handlebars
|
|
|
|
if (typeof partial === 'string') {
|
2014-01-23 20:22:25 +04:00
|
|
|
hbs.registerPartial(partial);
|
2013-12-02 03:31:55 +04:00
|
|
|
}
|
2013-11-28 06:45:01 +04:00
|
|
|
|
2015-03-28 19:00:57 +03:00
|
|
|
return new hbs.handlebars.SafeString(partial(context, options));
|
2013-11-28 06:45:01 +04:00
|
|
|
};
|
|
|
|
|
2014-02-23 06:16:07 +04:00
|
|
|
// Given a theme object and a post object this will return
|
|
|
|
// which theme template page should be used.
|
|
|
|
// If given a post object that is a regular post
|
|
|
|
// it will return 'post'.
|
|
|
|
// If given a static post object it will return 'page'.
|
|
|
|
// If given a static post object and a custom page template
|
|
|
|
// exits it will return that page.
|
|
|
|
templates.getThemeViewForPost = function (themePaths, post) {
|
|
|
|
var customPageView = 'page-' + post.slug,
|
|
|
|
view = 'post';
|
|
|
|
|
|
|
|
if (post.page) {
|
|
|
|
if (themePaths.hasOwnProperty(customPageView + '.hbs')) {
|
|
|
|
view = customPageView;
|
|
|
|
} else if (themePaths.hasOwnProperty('page.hbs')) {
|
|
|
|
view = 'page';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return view;
|
|
|
|
};
|
|
|
|
|
2014-09-05 20:35:30 +04:00
|
|
|
// Given a theme object and a tag slug this will return
|
|
|
|
// which theme template page should be used.
|
|
|
|
// If no default or custom tag template exists then 'index'
|
|
|
|
// will be returned
|
|
|
|
// If no custom tag template exists but a default does then
|
|
|
|
// 'tag' will be returned
|
|
|
|
// If given a tag slug and a custom tag template
|
|
|
|
// exits it will return that view.
|
|
|
|
templates.getThemeViewForTag = function (themePaths, tag) {
|
|
|
|
var customTagView = 'tag-' + tag,
|
|
|
|
view = 'tag';
|
|
|
|
|
|
|
|
if (themePaths.hasOwnProperty(customTagView + '.hbs')) {
|
|
|
|
view = customTagView;
|
|
|
|
} else if (!themePaths.hasOwnProperty('tag.hbs')) {
|
|
|
|
view = 'index';
|
|
|
|
}
|
|
|
|
|
|
|
|
return view;
|
|
|
|
};
|
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
module.exports = templates;
|