问题描述
我正在尝试让Mustache与Express一起正常工作,并且可以猜到,我遇到了麻烦.
I'm trying to get Mustache working properly with Express, and as one can guess, I'm having troubles.
以下代码行将小胡子漂亮且整洁地初始化.变量将按预期方式呈现.
The following line initializes Mustache nice and clean. Variables render as expected.
app.register('html', require(__dirname+'/public/js/libs/mustache.js'));
但是,当将部分投掷混合在一起时,问题开始出现.使用Mustache时,此部分"应该调用标头视图/部分.
However, the problems start to rise when partials are thrown in the mix. With Mustache, this here partial should invoke header view/partial.
{{> header}}
但是可惜没有任何反应. ://即使我直接提供部分,Mustache也无法渲染它.
But alas, nothing happens. :/ Even when I offer the partial directly, Mustache fails to render it.
app.get('/', function(req, res) {
res.render('welcome', {
partials: {
header: '<h1>Header</h1>'
}
});
});
因此,似乎这些分词根本没有用.我发现一个可以使部分运行正常的hack:
So, it seems that the partials aren't working at all. I've found one hack that get's the partials somewhat working:
http://bitdrift.com/post/2376383378/using- Mustache-Templates-in-Expression
之后,直接在渲染调用中提供局部视图时(参见上文),但是仍然无法将局部视图直接呈现给视图/布局:
After that the partials render when offered directly at render call (see above), but it still fails on rendering the partials directly to views/layouts:
布局看起来像这样:
Behold, the header
{{> header}}
<p>Let's move to more important stuff...</p>
标题视图如下:
<h1>Header</h1>
Express可以很好地自行加载视图,但是它不知道如何处理Mustache部分...
Express can load the views by themselves just ok, but it doesn't know how to handle the Mustache partials...
推荐答案
设法将其与最新版本的hogan-express配合使用.
Managed to get this working with the latest version of hogan-express.
https://github.com/vol4ok/hogan-express
所需要做的就是安装hogan-express并将其用作express上的模板引擎.无需黑客或调优.
All that is needed is to install hogan-express and use it as template engine on express. No hacking or tuning required.
这篇关于Node.js + Express-如何使Moustache部分正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!