对于techno 主题,我想为用户提供自定义的 hb 助手和配置。为此,我对 [ghost root]/index.js 应用了覆盖。
下面的代码在当前主题文件夹中搜索 index.js 并运行它。
var ghost = require('./core'),
errors = require('./core/server/errorHandling');
ghost()
.then(function (param) {
var settings = require('./core/server/api').settings;
settings
.read({key: 'activeTheme', context: {internal: true}})
.then(function (result) {
try {
require('./content/themes/' + result.value + '/index')();
}
catch (e) {
//No custom index found, or it wasn't a proper module.
}
});
})
.otherwise(function (err) {
errors.logErrorAndExit(err, err.context, err.help);
});
主题级别 index.js 注入自定义博客变量(来自配置文件)和 hb 助手。
var hbs = require('express-hbs'),
_ = require('lodash'),
downsize = require('downsize'),
blogVariable = require('../../../core/server/config/theme');
module.exports = function() {
//This block allows configuration to be available in the hb templates.
var blogConfig = blogVariable();
var config = require('./config') || {};
blogConfig.theme = config;
//console.log(JSON.stringify(blogConfig));
////Custom hb helpers////
hbs.registerHelper('excerpt', function (options) {
...
return new hbs.handlebars.SafeString(excerpt);
});
...
};
下面是使用自定义博客变量的示例。
<ul class="list-inline">
<li><a href="{{@blog.theme.author.github}}" class="btn-social btn-outline" data-toggle="tooltip" data-placement="top" title="Github"><i class="fa fa-fw fa-github"></i></a>
</li>
...
在 Ghost 0.4.2 中有没有更好的方法来做到这一点?我不喜欢让用户覆盖 ghost core index.js 文件。