How to use dynamic page titles in sails.js >v1.0?


For the last few days I was looking for a viable solution in order to optimize html page titles <title>SOME_TITLE</title> within sails.js layout files, like layout.ejs, that by default use a static page title. <br /> Obviously, it would be way better to have dynamic page titles, e.g. Dashboard, Shopping Cart, etc... <br /> Other people were looking for this answer before and got answers for prior sails versions in <a href="https://stackoverflow.com/questions/31148841/how-can-i-set-different-title-values-for-each-template-in-ejs" rel="nofollow">Solution 1</a>, <a href="https://stackoverflow.com/questions/20267550/what-is-the-proper-way-to-integrate-dynamic-content-into-the-layout-ejs-file-in" rel="nofollow">Solution 2</a> and <a href="https://stackoverflow.com/questions/18292415/where-is-title-or-app-name-for-sails-js-using-node-js-and-express-js" rel="nofollow">Solution 3</a>. Unfortunately, none of them seem to be appropriate for the latest version of sails.js (as of this post). <a href="https://stackoverflow.com/questions/31148841/how-can-i-set-different-title-values-for-each-template-in-ejs" rel="nofollow">Solution 1</a> was leading in the right direction and suggested what I was looking for. But you had to define a title for every controller and pass it into the view. Otherwise you will get


title is not defined at eval


So how to define a local variable that is accessible in each controller/view by default?


So one working complete solution for the current sails.js version is the following:<br />

In your layout.ejs file define a dynamic page title like this

<head> <title> <%= title %> </title> ... </head> ...

Create a new custom hook, e.g. api/hooks/dynamic-page-title/index.js

module.exports = function dynamicPageTitleHook(sails) { return { routes: { /** * Runs before every matching route. * * @param {Ref} req * @param {Ref} res * @param {Function} next */ before: { '/*': { skipAssets: true, fn: async function(req, res, next){ // add page title variable to each response if (req.method === 'GET') { if (res.locals.title === undefined) { res.locals.title = 'plusX'; } } return next(); } } } } }; };

Now overwrite the page title in every controller that should use a custom page title, e.g. view-login.ejs

module.exports = { friendlyName: 'View login', description: 'Display "Login" page.', exits: { success: { viewTemplatePath: 'pages/entrance/login', }, redirect: { description: 'The requesting user is already logged in.', responseType: 'redirect' } }, fn: async function (inputs, exits) { if (this.req.me) { throw {redirect: '/'}; } return exits.success({title: 'Login'}); } };


  • R Remove zeros at the start and end of a column
  • Can a structure tag be used before its scope?
  • Custom URI routing by query string with CodeIgniter?
  • ansible playbook unable to continue as the `tar` fails due to `file change as we read`
  • How to wait till webViewDidFinishLoad gets completed
  • how to calculate distance between two points in google maps?
  • How can I merge my files when the folder structure has changed using Borland StarTeam?
  • Put percent labels next to legend instead of in the slice
  • HABTM associations in Rails : collecting and counting the categories of a model's children
  • Single django queryset to get n adjacent items
  • Guava how to copy all files from one directory to another
  • Criterion causing memory consumption to explode, no CAFs in sight
  • Retaining data after updating application
  • How to handle elastic beanstalk deployment so it uploads only changed files
  • How to get links to open in the native browser in iOS Meteor apps?
  • how to set variables in a php include file?
  • Filter strings with regex before casting to numeric
  • gspread or such: help me get cell coordinates (not value)
  • How to define custom class, title, and target in Link Browser for content elements and the new rte_c
  • How to define and use opencv mat of user type
  • Sencha Touch 2.0 Controller refs attribute not working?
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Extracting HTML between tags
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • Nant, Vault & Windows Integrated Authentication
  • What is the “return” in scheme?
  • SSO with signing and signature validation doesn't work
  • Deserializing XML into class C#
  • vba code to select only visible cells in specific column except heading
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • How to include full .NET prerequisite for Wix Burn installer
  • log4net write single file for each call to log.info
  • Free memory of cv::Mat loaded using FileStorage API
  • Memory offsets in inline assembly
  • Cant find why the layout is getting smaller
  • Turn off referential integrity in Derby? is it possible?
  • costura.fody for a dll that references another dll
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize