Use layout.ejs dynamic or how to edit a view-layout.js

178 Views Asked by At

Every Page which will be included in the layout.ejs has a view-pagename.js so I can use dynamic content in it. How can I bring dynamic content a a default function to the layout page? There is no view-layout.js

1

There are 1 best solutions below

0
Jorge Vargas On BEST ANSWER

You could use hooks where you can get data from helpers or even from models. Check the routes helper specification:

https://sailsjs.com/documentation/concepts/extending-sails/hooks/hook-specification/routes

So you can specify a hook using sails generate hook some-hook-name and use it:

module.exports = function (sails) {
  return {

    initialize: function(cb) {
      sails.log.info('Hook some-hook-name loaded successfully');
    },

    routes: {
      before: {
        'GET /*': async function (req, res, next) {
          res.locals.pageTitle = await sails.helpers.getPageTitle(req);
          
          return next();
        }
      },
    }
  };
};

After that you can use it at the views:

<%= pageTitle %>

And there is an example to set variables as locals:

https://github.com/mikermcneil/ration/blob/master/api/hooks/custom/index.js#L240