问题
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