Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partials as an Option #82

Closed
bigorangemachine opened this issue Sep 9, 2014 · 3 comments · Fixed by #107
Closed

Partials as an Option #82

bigorangemachine opened this issue Sep 9, 2014 · 3 comments · Fixed by #107

Comments

@bigorangemachine
Copy link

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
Copy link
Owner

ericf commented Sep 11, 2014

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
Copy link
Owner

ericf commented Sep 11, 2014

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

@bigorangemachine
Copy link
Author

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

ericf added a commit that referenced this issue Feb 17, 2015
The `renderView()` method now accepts render-level `partials` that are
merged with the instance-level and global Handlebars partials. The
option value is specified as object with the shape: `{partialName: fn}`
or a Promise of such an object.

Fixes #82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants