如何使用 ejs 模板在 Sailsjs 中设置元描述标签?

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

我希望能够从风帆控制器操作中设置描述元标记。我到处搜索,但唯一的例子涉及页面标题。这是我的第一个 node.js 和 sailsjs 站点,我是不是用错了方法? 像这样:

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

是的,这是正确的。您可以使用

在您的模板中插入
<!DOCTYPE html>
<html>
  <head>
  <title><%= title %></title>
  <meta name="description" content="<%= metaDescription %>">

可在此处找到文档和示例:

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

感谢@Bulkin 为我指明了正确的方向。这是对我有用的解决方案。我将局部变量放在布局模板中,它通过从控制器为所有页面(但主页除外)传递元标记文本来工作。主页不断抛出错误,因为看到 "metaDescription",未定义。解决方法是在 config/routes.

的主页路由中设置局部变量
module.exports.routes = {
  '/': {
  view: 'home/index',
  locals: {
    metaDescription: "Description Text"
  }
 }
};