0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/core/server/controllers/frontend/templates.js
Hannah Wolfe bb3cc8c0f8 Reimplement custom theme templates (#8147)
closes #8082

- Update the `pickTemplate` logic to
  a) rely on getActive().hasTemplate() instead of being passed a list of paths
  b) support the concept of a fallback, which is returned if there is no theme, or if the theme doesn't have a more specific template
- Update every instance of template picking, across the 3 internalApps, and render-channel, to use this new logic
- update the tests
2017-03-14 00:15:50 +01:00

110 lines
3 KiB
JavaScript

// # Templates
//
// Figure out which template should be used to render a request
// based on the templates which are allowed, and what is available in the theme
var _ = require('lodash'),
themes = require('../../themes');
/**
* ## Get Channel Template Hierarchy
*
* Fetch the ordered list of templates that can be used to render this request.
* 'index' is the default / fallback
* For channels with slugs: [:channelName-:slug, :channelName, index]
* For channels without slugs: [:channelName, index]
* Channels can also have a front page template which is used if this is the first page of the channel, e.g. 'home.hbs'
*
* @param {Object} channelOpts
* @returns {String[]}
*/
function getChannelTemplateHierarchy(channelOpts) {
var templateList = ['index'];
if (channelOpts.name && channelOpts.name !== 'index') {
templateList.unshift(channelOpts.name);
if (channelOpts.slugTemplate && channelOpts.slugParam) {
templateList.unshift(channelOpts.name + '-' + channelOpts.slugParam);
}
}
if (channelOpts.frontPageTemplate && channelOpts.postOptions.page === 1) {
templateList.unshift(channelOpts.frontPageTemplate);
}
return templateList;
}
/**
* ## Get Single Template Hierarchy
*
* Fetch the ordered list of templates that can be used to render this request.
* 'post' is the default / fallback
* For posts: [post-:slug, post]
* For pages: [page-:slug, page, post]
*
* @param {Object} single
* @returns {String[]}
*/
function getSingleTemplateHierarchy(single) {
var templateList = ['post'],
type = 'post';
if (single.page) {
templateList.unshift('page');
type = 'page';
}
templateList.unshift(type + '-' + single.slug);
return templateList;
}
/**
* ## Pick Template
*
* Taking the ordered list of allowed templates for this request
* Cycle through and find the first one which has a match in the theme
*
* @param {Array|String} templateList
* @param {String} fallback - a fallback template
*/
function pickTemplate(templateList, fallback) {
var template;
if (!_.isArray(templateList)) {
templateList = [templateList];
}
if (!themes.getActive()) {
template = fallback;
} else {
template = _.find(templateList, function (template) {
return themes.getActive().hasTemplate(template);
});
}
if (!template) {
template = fallback;
}
return template;
}
function getTemplateForSingle(single) {
var templateList = getSingleTemplateHierarchy(single),
fallback = templateList[templateList.length - 1];
return pickTemplate(templateList, fallback);
}
function getTemplateForChannel(channelOpts) {
var templateList = getChannelTemplateHierarchy(channelOpts),
fallback = templateList[templateList.length - 1];
return pickTemplate(templateList, fallback);
}
module.exports = {
channel: getTemplateForChannel,
single: getTemplateForSingle,
pickTemplate: pickTemplate
};