How can I set a meta description tag in Sailsjs using ejs templates?

好久不见. 提交于 2019-12-11 02:44:06

问题


I would like to be able to set the description meta tag from the sails controller action. I have searched all over, but the only example involves the page title. This is my first node.js and sailsjs site, am I going about this the wrong way? Something like this:

module.exports = {
index: function (){
res.view(
  {
    title: 'The Title Text', 
    metaDescription: "The Description Text"
  });
  }
};

回答1:


Yes, this is correct. You can insert in your template with

<!DOCTYPE html>
<html>
  <head>
  <title><%= title %></title>
  <meta name="description" content="<%= metaDescription %>">

Docs and examples can be found here:

http://sailsjs.org/#!/documentation/concepts/Views/Locals.html http://sailsjs.org/#!/documentation/reference/res/res.view.html




回答2:


Thank you @Bulkin for pointing me in the right direction. Here is the solution that worked for me. I put the local variable in the layout template and it worked by passing the meta tag text from the controller for all pages, but the home page. The home page kept throwing an error as seeing "metaDescription", as undefined. The fix was to set the local variable in the home page route of config/routes.

module.exports.routes = {
  '/': {
  view: 'home/index',
  locals: {
    metaDescription: "Description Text"
  }
 }
};


来源:https://stackoverflow.com/questions/30603376/how-can-i-set-a-meta-description-tag-in-sailsjs-using-ejs-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!