简体   繁体   English

如何使用Express,Handlebars和Conslidate在Node.js中设置部分路径

[英]How to set path to partials in Node.js with Express, Handlebars and Conslidate

I have a working Node.js site, using Express.js, Handlebars.js and Consolidate.js. 我有一个工作的Node.js站点,使用Express.js,Handlebars.js和Consolidate.js。 I want to use partials for common parts of my templates, but can't work out how to make them work for pages at different URLs. 我想对我的模板的常见部分使用partials,但无法弄清楚如何使它们适用于不同URL的页面。

My /views/ directory contains this: 我的/views/目录包含这个:

_footer.html
_header.html
article.html
index.html

The relevant parts of my Node app looks something like: 我的Node应用程序的相关部分类似于:

var express = require('express'),
    consolidate = require('consolidate'),
    handlebars = require('handlebars'),
    path = require('path');
var app = express();
app.engine('html', consolidate.handlebars);
app.set('view engine', 'html');
app.set('views', path.join(__dirname, '/views'));

var partials = {header: '_header', footer: '_footer'};

app.get('/', function(req, res) {
    res.render('index', {partials: partials});
};
app.get(/^\/([\w-]+)\/$/, function(req, res) {
    res.render('article', {partials: partials});
};

And in my index.html and article.html Handlebars templates I have something like: 在我的index.htmlarticle.html Handlebars模板中,我有类似的东西:

{{> header}}
<!-- page content here -->
{{> footer }}

I should be able to access both / (when index.html is rendered) and /foo/ (when article.html is rendered). 我应该能够访问/ (当呈现index.html时)和/foo/ (当呈现article.html时)。 But it only works for whichever I try to access first after starting the Node server. 但它只适用于我在启动Node服务器后首先尝试访问的任何一个。 When I then navigate to the other path, I get errors like: 当我然后导航到另一条路径时,我得到的错误如下:

Error: ENOENT, no such file or directory '/Users/phil/Projects/projectname/views/<!DOCTYPE html>...

with the rest of the _header.html partial following. 其余的_header.html部分跟随。

I assume I need to somehow set the path to my partials to be absolute somehow, but I can't see how to do that. 我假设我需要以某种方式设置我的部分路径是绝对的,但我不知道如何做到这一点。

For consolidate check that : https://github.com/tj/consolidate.js/issues/18 对于合并检查: https//github.com/tj/consolidate.js/issues/18

I would recommend to switch to something a bit more specific like that https://github.com/donpark/hbs it will be simpler. 我建议切换到更具体的东西,如https://github.com/donpark/hbs它会更简单。

I had the same exact issue. 我有同样的问题。 It seems that consolidate is reusing the "partials" object that you pass , replacing the values with the file content (yuck!). 似乎合并正在重用你传递的“部分”对象 ,用文件内容替换值(哎呀!)。 A workaround is to create a new "partials" object every time. 解决方法是每次都创建一个新的“部分”对象。 If you don't want to rewrite the whole object every time, you can use a function returning the object literal. 如果您不想每次都重写整个对象,则可以使用返回对象文字的函数。 In your case something like the following should work: 在你的情况下,类似下面的东西应该工作:

var express = require('express'),
    consolidate = require('consolidate'),
    handlebars = require('handlebars'),
    path = require('path');
var app = express();
app.engine('html', consolidate.handlebars);
app.set('view engine', 'html');
app.set('views', path.join(__dirname, '/views'));

var partials = function() {
    return {header: '_header', footer: '_footer'};
}

app.get('/', function(req, res) {
    res.render('index', {partials: partials()});
};
app.get(/^\/([\w-]+)\/$/, function(req, res) {
    res.render('article', {partials: partials()});
};

Not really elegant, but I don't think there is really a way to keep it simpler. 不是很优雅,但我认为没有办法让它变得更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM