pekpon
pekpon

Reputation: 769

Sails.js load object in all views

I have a simple website with a main menu. The menu has categories like "T-Shirts, Shoes and Accessories...". But its dynamic. All of these categories are in a Model "Category" and the words can be different.

So, how can I have this menu rendered on every page without passing data to the view on every controller like:

Category.find(function(err, categories){
    res.view({categories: categories});
});

Thanks!

Upvotes: 0

Views: 203

Answers (2)

user4707474
user4707474

Reputation:

If you want it on all pages, it might be easiest to write a customer middleware function in config/http.js that adds categories to the local variables.

See sails docs - adding-or-overriding-http-middleware

Upvotes: 0

kailash yogeshwar
kailash yogeshwar

Reputation: 834

Define your header in a separate file call it as header.html and include it in layout.ejs using

<% include ../includes/header.html %>

And show appropriate menus on each page depending on the condition whether it should be displayed to normal user or admin by adding roles field to category collection.

Upvotes: 0

Related Questions