In Sails, views are markup templates that are compiled on the server into HTML pages. In most cases, views are used as the response to an incoming HTTP request, e.g. to serve your home page.
Alternatively, a view can be compiled directly into an HTML string for use in your backend code (see sails.renderView()
.) For instance, you might use this approach to send HTML emails, or to build big XML strings for use with a legacy API.
By default, Sails is configured to use EJS (Embedded Javascript) as its view engine. The syntax for EJS is highly conventional- if you've worked with php, asp, erb, gsp, jsp, etc., you'll immediately know what you're doing.
If you prefer to use a different view engine, there are a multitude of options. Sails supports all of the view engines compatible with Express via Consolidate.
Views are defined in your app's views/
folder by default, but like all of the default paths in Sails, they are configurable. If you don't need to serve dynamic HTML pages at all (say, if you're building an API for a mobile app), you can remove the directory from your app.
Anywhere you can access the res
object (i.e. a controller action, custom response, or policy), you can use res.view
to compile one of your views, then send the resulting HTML down to the user.
You can also hook up a view directly to a route in your routes.js
file. Just indicate the relative path to the view from your app's views/
directory. For example:
{
'get /': {
view: 'homepage'
},
'get /signup': {
view: 'signupFlow/basicInfo'
},
'get /signup/password': {
view: 'signupFlow/chooseAPassword'
},
// and so on.
}
If you are building a web application for the browser, part (or all) of your navigation may take place on the client; i.e. instead of the browser fetching a new HTML page each time the user navigates around, the client-side code preloads some markup templates which are then rendered in the user's browser without needing to hit the server again directly.
In this case, you have a couple of options for bootstrapping the single-page app:
views/publicSite.ejs
. Advantages:assets/index.html
. Advantages: