如何在Sails.js中为Handlebars配置部分和布局? [英] How to configure partials and layouts for Handlebars in Sails.js?
问题描述
我运行 Sails 0.9.7
并安装了把手 .com / visionmedia / consolidate.jsrel =noreferrer> Consolidate.js ,因此 Sails支持
I run Sails 0.9.7
and have installed Handlebars which is supported by Consolidate.js and therefore is supported by Sails
我可以从 .handlebars
文件,它工作得很好。
我无法在Sails工作流和 Sails方式中找到位置,我应该注册partials,helpers等......我更多地寻找最佳实践,而不仅仅是一个工作解决方案,但任何帮助将不胜感激。
I can serve pages from .handlebars
files, it works just fine.
I can't figure where, in Sails workflow, and in a Sails way, I should register partials, helpers etc... I'm more looking for best practices than just a working solution but any help will be appreciated.
推荐答案
我正在运行v0.10 beta,但这不应该影响我如何在以下工作:
I'm running v0.10 beta but this shouldn't affect how I got it working below:
- 引擎应该像预期的那样是 handlebars
- 路线需要明确定义控制器和动作。设置视图不起作用。 (除非有什么办法,我不能在路径文件中设置partials)
- 控制器需要将partials定义为相对于视图的路径。
config / views.js
module.exports.views = {
engine : 'handlebars',
layout : false
};
config / routes.js
'/': {
controller: 'site',
action: 'index'
},
SiteController.js
SiteController.js
module.exports = {
// Render Index View
index: function(req, res) {
res.view({
partials: {
head: 'partials/head',
tail: '../partials/tail',
},
});
}
};
views / site / index.handlebars $ b
views/site/index.handlebars
{{> head}}
<h3>SITE INDEX</h3>
views / site / partials / head.handlebars
<h1>HEAD</h1>
{{> tail}}
views / partials / tail.handlebars
<h2>HEAD TAIL</h2>
OUTPUT
OUTPUT
<h1>HEAD</h1>
<h2>HEAD TAIL</h2>
<h3>SITE INDEX</h3>
这篇关于如何在Sails.js中为Handlebars配置部分和布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!