Sto utilizzando il file handlebars hbs wrapper in express.js. Ho dei modelli che funzionano bene, ma ho bisogno di aggiungere parziali da renderizzare con le mie viste.Modulo hbs di Express.js - registrazione parziali dal file .hbs
mi piacerebbe fare qualcosa di simile:
hbs.registerPartial('headPartial', 'header');
// where "header" is an .hbs file in my views folder
Tuttavia, è gettare un "header parziale non può essere trovato".
Posso rendere il lavoro registerPartial se si passa una stringa di html al secondo param, ma mi piacerebbe utilizzare file di visualizzazione separati per i miei parziali.
Non ho trovato alcuna documentazione su questo, ma spero che mi manchi solo qualcosa di facile.
Qualcuno sa come utilizzare i file di visualizzazione nel metodo registerPartial? Se sì, come posso implementarlo?
UPDATE
per dare più contesto, permettetemi di aggiungere più codice. Ecco il mio file "server" - 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);
e qui è il mio file routes.index:
exports.index = function(req, res){
res.render('index', { title: 'Express' })
};
Nella mia cartella di vista, ho tre modelli:
views/
header.hbs (this is my partial)
index.hbs
layout.hbs
Nel mio file index.hbs, sto chiamando il partial 'headPartial' con:
{{> headPartial}}
Qualsiasi aiuto è molto apprezzato.
Nizza. Un modo rapido per avere tutti i partial disponibili quando necessario! – swatkins
Grazie Ben, questo mi ha davvero aiutato molto. – Dave