ericf / express-handlebars

A Handlebars view engine for Express which doesn't suck.
BSD 3-Clause "New" or "Revised" License
2.31k stars 383 forks source link

Partials as an Option #82

Closed bigorangemachine closed 9 years ago

bigorangemachine commented 10 years ago

Hi, Thanks for the great work on express-handlebars.

I'm a little new to Node/Express/Handlebars but I have lots of javascript experience.

I'm trying to experiment and see if I can change a partial when you do a res.render()

My 'Main()' node script

//declare statics
var TPL_DIR=__dirname+'/'+'handlebars_tpl/',
    TPL_PRTLS='partials/',
    TPL_LYT='layouts/',
    TPL_DEFAULT=TPL_DIR+TPL_LYT+'base.html';
//declare locals
var express = require('./node_modules/express'),
    _ = require('./node_modules/underscore'),
    exphbs = require('./node_modules/express-handlebars'),//some call this exphbs
    app = express(),
    hbs = exphbs.create({'extname':'html', 'defaultLayout':TPL_DEFAULT,'layoutsDir':TPL_DIR+TPL_LYT,'partialsDir':TPL_DIR+TPL_PRTLS});

//declare globals (utilities)
global.bdUtls = require('./utls/bd');
global.HandleBars = require('./node_modules/handlebars');
global.exphbs = exphbs;
global.hbs = hbs;
global.page_vars={
    'db_obj':false,
    'assets_url':'',
    'base_url':''
};

//set global methods
app.set('views', TPL_DIR+TPL_LYT);
app.engine('.html', hbs.engine);
app.set('view engine', '.html');
app.all('*', function(req, res, next){//always do this
    global.page_vars.base_url=req.protocol + '://' + req.get('host') + req.originalUrl;
    var env = process.env.NODE_ENV || 'default', cfg = require('./config/'+env);
    global.page_vars.assets_url='//'+global.bdUtls.url_chomp(cfg.assets_url);//remove https:// http:// or //
    _.extend(hbs.helpers,{//new helpers!
        'page_vars':global.page_vars,
        'assets_url':global.page_vars.assets_url
    });

    next();//this is required or it hangs!  This allows other 'routes' to happen
});

//express stuff for routing
app.listen(8080,function(){//on start
    //process.stdout.write("\u001B[2J\u001B[0;0f");//clear console
    console.log('listening to port: 8080');
});
var demo_routes = require('./hello.world');
app.get('/', demo_routes.action);
app.get('/hello(\.|\/)world', demo_routes.action);
app.post('/hello(\.|\/)world', demo_routes.action);//POST!

My Router Rule (hello.world):

var demo_route_func=function(req,res) {
    //res.render('home');
console.log(global.hbs);
    res.render('hello.world.html',{'helpers':{'debug_output':new global.HandleBars.SafeString('<p>test</p>')},'partials':{'footer':'altfooter.html','header':'altheader.html'}});
    //res.send('Hello Bold New World');
};
exports.action = demo_route_func;

Is this:

res.render('hello.world.html',{'helpers':{'debug_output':new global.HandleBars.SafeString('<p>test</p>')},'partials':{'footer':'altfooter.html','header':'altfooter.html'}});

setup to work or am I doing it wrong? It finds the partial 'header' or 'footer' correctly. It seems you can't switch the partial when you flip the 'render'. Maybe its my n00bness to handlebars but I thought you could do pass the partials as an argument or option.

ericf commented 9 years ago

That feature is currently not supported. Right now you must configure the dirs where your partials are up front.

Stepping back, what are you trying to accomplish with having a dynamic partial? There might be a different way of accomplishing what you're trying to do. And in the meantime, I'll think through how I could add support for passing in partials to res.render().

ericf commented 9 years ago

I want to figure out how Handlebars behaves when you use registerPartial(), but also render a template passing {partials: {...}} as the options argument.

bigorangemachine commented 9 years ago

Eric, Thanks for your reply.

I am experimenting. I am trying to figure out which features overlap or if I could hack it :p

I am new to handlebars to so there was confusion around other issues I resolved. This was the only problem I couldn't resolve because changing the core handlebars code didn't give me debugging info I was trying to get.

I went through the express-handlebars code and I saw everything was populating correctly. In a weird way I am happy that it wasn't me.

Thanks again Eric