Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js - How to render a partial from a controller?

I have a partial view containing my login form. I would like to render it from an ajax call to my controller.

This is the sample where i would return my partial view:

postlogin: function (req,res) {

    var username = req.param('username');
    var password = req.param('password');

    User.find({
        username: username,
        password: password.salt()
    }).done(function(err, users){
        if(users.length == 1){
            // Here I want to return a partial view, not a view
            res.view('home/login', {message: 'Login success!'});
        }else{
            // Here I want to return a partial view, not a view
            res.view('home/login', {message: 'Login failed!'});
        }
    });
  },
like image 283
Samuel Poirier Avatar asked Aug 15 '13 19:08

Samuel Poirier


2 Answers

Ah! Found it!

If your view is a partial view, simply specify layout: null:

res.view('home/login', {message: 'Login failed!', layout: null});
like image 89
Samuel Poirier Avatar answered Nov 15 '22 18:11

Samuel Poirier


Bottom of the page: http://sailsjs.org/#!documentation/views

What about using multiple layouts?

Express 3 removed native support for layouts. In Sails, we've managed to keep this around, but we don't officially support multiple layouts. That said, at least in EJS, instead of indicating your custom layout with the layout local, you must use _layoutFile:

/**
 * HomeController
 */
module.exports = {

    index: function (req, res) {
        res.view({
            _layoutFile: '../layouts/other.ejs'
        });
    },

};

Sails v0.9.7

like image 45
Travis Avatar answered Nov 15 '22 20:11

Travis