Sunday, 19 March 2017

Show flash messages using vash view engine

is it possible to show a flash message using vash view engine?

code from my app.js file :

...

var flash = require('connect-flash');
var index = require('./routes/index');
var users = require('./routes/users');

var db = require('./db'); 
var app = express();

...

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

code from my users.js file :

...

 router.get('/', function(req,res,next){
      req.flash('info', 'Flash is back!')
      res.render('index',{greet : "Hello world"});

    });

code from my layout.vash

...

 <div id="bodyrender">
        <div class="container">

        @{ messages() }

        @html.block('renderbody', function(){})
        </div>
        }
    </div>

code from my index.vash : ...

@html.extend('layout', function(model) {
  @html.block('renderbody', function(model) {

    @if(@model.greet){

            <h3>@model.greet</h3><br/>

        }
  })
})

This error occures :

ReferenceError: Problem while rendering template at line 45, character 0.
Original message: Problem while rendering template at line 45, character 0.
Original message: messages is not defined.
Context: 

   43 |         <div class="container">
   44 |         
 > 45 |         @{ messages() }
   46 |           
   47 |         @html.block('renderbody', function(){})
   48 |         </div>

I'd like to know what can I do to let "messages()" function runs as it's required on "express-messages" Readme.md file like this :

Rendering Messages

Call the messages() function as specified by your rendering engine:

EJS:

<%- messages() %>

Jade:

!= messages()



via Dev Walker

No comments:

Post a Comment