Używam pliku handlebars.js hbs wrapper w express.js. Mam szablony działające poprawnie, ale muszę dodać części, które mają być renderowane z moimi widokami.Moduł Express.js hbs - zarejestruj partials z pliku .hbs
chciałbym zrobić coś takiego:
hbs.registerPartial('headPartial', 'header');
// where "header" is an .hbs file in my views folder
Jednak to rzucanie „header częściowe nie można znaleźć”.
Mogę zrobić registerPartial pracy, jeśli przekazuję ciąg html do drugiego param, ale chciałbym użyć osobnych plików widoku dla moich części.
Nie znalazłem żadnej dokumentacji na ten temat, ale mam nadzieję, że może mi brakować czegoś łatwego.
Czy ktoś wie, jak korzystać z plików widoku w metodzie registerPartial? Jeśli tak, w jaki sposób mogę to wdrożyć?
UPDATE
Aby dać więcej kontekstu, dodam więcej kodu. Oto mój "server" plik - app.js
var express = require('express')
, routes = require('./routes')
, hbs = require('hbs');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'hbs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// this is the line that generates the error
hbs.registerPartial('headPartial', 'header');
// What I'm expecting is for "headPartial" to be a compiled template partial
// of the template within views/header.hbs, but it is not loading this way.
// If I do something like hbs.registerPartial('headPartial', '<p>test</p>');
// then it does work. I need to know how to pass an .hbs file to the
// registerPartial method.
// Routes
app.get('/', routes.index);
app.listen(3000);
I tu jest moje routes.index file:
exports.index = function(req, res){
res.render('index', { title: 'Express' })
};
W moim folderze poglądów, mam trzy szablony:
views/
header.hbs (this is my partial)
index.hbs
layout.hbs
W pliku index.hbs nazywam części "headPartial" z:
{{> headPartial}}
Każda pomoc jest bardzo doceniana.
Nicei. Szybki sposób na udostępnienie wszystkich częściowych w razie potrzeby! – swatkins
Dzięki Ben, to naprawdę bardzo pomogło. – Dave