visionmedia / express-messages

Express flash notification message rendering
261 stars 41 forks source link

Use this with Handlebars? #13

Closed andeersg closed 8 years ago

andeersg commented 9 years ago

Is it possible to use this module with handlebars as a view engine?

I'm not sure how to implement it.

ERPedersen commented 8 years ago

bump

di-ego02 commented 8 years ago

when you utilize this codeapp.use(require('connect-flash')()); app.use(function (req, res, next) { res.locals.messages = require('express-messages')(req, res); next(); }); taken from the documentation here it specifies that you need to invoke res.local.messages in your template, i e <%-messages()%> but in handlebars you can't write js in the curly braces. So we must call res.local.messages before hand, i e res.locals.messages = require('express-messages')(req, res)(); which will return the flash message object! in your handlebar template you access the key 'messages' like this {{{messages}}}

artcommacode commented 8 years ago

Thanks @di-ego02, can you confirm this work for you @andeersg, @ERPedersen?

andeersg commented 8 years ago

Yes, I have done something similar (or exactly the same I see):

app.use(flash());
app.use(function (req, res, next) {
  res.locals.messages = require('express-messages')(req, res);
  next();
});

So I guess we can close this :)

artcommacode commented 8 years ago

Thanks for the confirmation.

ERPedersen commented 8 years ago

Works for me too :) I did somewhat the same :+1:

artcommacode commented 8 years ago

Good to hear, I'll try and add it to the docs once I get the chance.

Gauravkandpal001 commented 6 years ago

Thanks de-ego02 you solved my issue of using handlebars with express.

dalalRohit commented 6 years ago

@Gauravkandpal001 Can you show me how did you write the handlebars template code for this? I'm stuck on that only.

Gauravkandpal001 commented 6 years ago

@dalalRohit My error was I forgot to add ( ) at the end of below line of code. : res.locals.messages = require('express-messages')(req, res)();
in handlebars use : {{{message}}}
to display your message

{{{messages}}} {{#if errors}} {{#each errors}}
{{msg}}
{{/each}} {{/if}} {{{body}}}
{{>login}}