mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
f8b498d6e7
no issue 🎨 Switch themes API to use config.availableThemes - this gets rid of the only places where settings.availableThemes are used 🔥 Get rid of settings.availableThemes - this is no longer used anywhere - also get rid of every related call to updateSettingsCache 🔥 Replace config.availableThemes with theme cache - Creates a tailor-made in-memory cache for themes inside the theme module - Add methods for getting & setting items on the cache - Move all references to config.availableThemes to use the new cache - This can be abstracted later to support other kinds of caches? 🎨 Start improving theme lib's API Still TODO: simplifying/clarifying: - what is the structure of the internal list - what is the difference between a package list, and a theme list? - what is the difference between reading a theme and loading it? - how do we update the theme list (add/remove) - how do we refresh the theme list? (hot reload?!) - how do we get from an internal list, to one that is sent as part of the API? - how are we going to handle theme storage: read/write, such that the path is configurable 🎨 Use themeList consistently 🎨 Update list after storage
100 lines
2.8 KiB
JavaScript
100 lines
2.8 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'),
|
|
themeList = require('../../themes').list;
|
|
|
|
function getActiveThemePaths(activeTheme) {
|
|
return themeList.get(activeTheme);
|
|
}
|
|
|
|
/**
|
|
* ## 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 {Object} themePaths
|
|
* @param {Array} templateList
|
|
*/
|
|
function pickTemplate(themePaths, templateList) {
|
|
var template = _.find(templateList, function (template) {
|
|
return themePaths.hasOwnProperty(template + '.hbs');
|
|
});
|
|
|
|
if (!template) {
|
|
template = templateList[templateList.length - 1];
|
|
}
|
|
|
|
return template;
|
|
}
|
|
|
|
function getTemplateForSingle(activeTheme, single) {
|
|
return pickTemplate(getActiveThemePaths(activeTheme), getSingleTemplateHierarchy(single));
|
|
}
|
|
|
|
function getTemplateForChannel(activeTheme, channelOpts) {
|
|
return pickTemplate(getActiveThemePaths(activeTheme), getChannelTemplateHierarchy(channelOpts));
|
|
}
|
|
|
|
module.exports = {
|
|
getActiveThemePaths: getActiveThemePaths,
|
|
channel: getTemplateForChannel,
|
|
single: getTemplateForSingle
|
|
};
|