node.js - Express.js hbs module - register partials from .hbs file -
i'm using handlebars.js hbs wrapper in express.js. have templates working fine, i'm needing add in partials rendered views.
i'd this:
hbs.registerpartial('headpartial', 'header'); // "header" .hbs file in views folder however, it's throwing "header partial can not found".
i can make registerpartial work if pass string of html second param, i'd use separate view files partials.
i haven't found documentation on this, hoping may missing easy.
does know how use view files in registerpartial method? if so, how implement this?
update
to give more context, let me add more code. here "server" file - 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()); }); // line generates error hbs.registerpartial('headpartial', 'header'); // i'm expecting "headpartial" compiled template partial // of template within views/header.hbs, not loading way. // if hbs.registerpartial('headpartial', '<p>test</p>'); // work. need know how pass .hbs file // registerpartial method. // routes app.get('/', routes.index); app.listen(3000); and here routes.index file:
exports.index = function(req, res){ res.render('index', { title: 'express' }) }; in views folder, have 3 templates:
views/ header.hbs (this partial) index.hbs layout.hbs in index.hbs file, i'm calling 'headpartial' partial with:
{{> headpartial}} any appreciated.
this code loads partial templates in directory , makes them available filename:
var hbs = require('hbs'); var fs = require('fs'); var partialsdir = __dirname + '/../views/partials'; var filenames = fs.readdirsync(partialsdir); filenames.foreach(function (filename) { var matches = /^([^.]+).hbs$/.exec(filename); if (!matches) { return; } var name = matches[1]; var template = fs.readfilesync(partialsdir + '/' + filename, 'utf8'); hbs.registerpartial(name, template); });
Comments
Post a Comment