Javascript 动态设置元标记EJS
我正在使用EJS,我需要为每个帖子设置元标记。我在layouts文件夹中有一个样板文件,我把它放在每一页上。当用户进入发布页面时,我需要设置动态元标记和标题。 我的样板Javascript 动态设置元标记EJS,javascript,node.js,ejs,Javascript,Node.js,Ejs,我正在使用EJS,我需要为每个帖子设置元标记。我在layouts文件夹中有一个样板文件,我把它放在每一页上。当用户进入发布页面时,我需要设置动态元标记和标题。 我的样板 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title><%= title %></title> <meta name="viewp
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><%= title %></title>
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<% include ../partials/navbar.ejs %>
<div class="container-fluid">
<% include ../partials/filter.ejs %>
<div class="row">
<div class="col-sm-12 col-lg-10">
<%- body -%>
</div>
<div class="col-sm-12 col-lg-2">
<% include ../partials/sidebar %>
</div>
</div>
</div>
</body>
</html>
但我有一个错误,即样板中没有定义标题 如果我正确理解了这个问题,我为您制作了一个样本 帖子的mongoose模式
const postScheme = new Schema({
"title": String,
"description": String, // meta description
"robots": String, // index or noindex
"lang": String, // en, fr, tr
"pathname": String, // post-pathname
"main": String // post body content
})
获取post数据并进行渲染
Post.findOne({ _id: req.params.id }, (err, data) => {
res.render('post/index', { 'data': data })
}).lean()
并编辑.ejs文件
<!doctype html>
<html lang="<%= data.lang %>">
<head>
<meta charset="UTF-8">
<title> <%= data.title %> </title>
<meta name="description" content=" <%= data.description %> "/>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<% include ../partials/navbar.ejs %>
<div class="container-fluid">
<% include ../partials/filter.ejs %>
<div class="row">
<div class="col-sm-12 col-lg-10">
<%- data.main %>
</div>
<div class="col-sm-12 col-lg-2">
<% include ../partials/sidebar %>
</div>
</div>
</div>
</body>
</html>
如果我正确理解了问题,我为您制作了一个示例 帖子的mongoose模式
const postScheme = new Schema({
"title": String,
"description": String, // meta description
"robots": String, // index or noindex
"lang": String, // en, fr, tr
"pathname": String, // post-pathname
"main": String // post body content
})
获取post数据并进行渲染
Post.findOne({ _id: req.params.id }, (err, data) => {
res.render('post/index', { 'data': data })
}).lean()
并编辑.ejs文件
<!doctype html>
<html lang="<%= data.lang %>">
<head>
<meta charset="UTF-8">
<title> <%= data.title %> </title>
<meta name="description" content=" <%= data.description %> "/>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<% include ../partials/navbar.ejs %>
<div class="container-fluid">
<% include ../partials/filter.ejs %>
<div class="row">
<div class="col-sm-12 col-lg-10">
<%- data.main %>
</div>
<div class="col-sm-12 col-lg-2">
<% include ../partials/sidebar %>
</div>
</div>
</div>
</body>
</html>
是否在所有页面中呈现
标题
变量?如果不是,这就是为什么会出现未定义的错误。用locals
对象检查未定义的变量,并将变量包含在标题html标记中。如果不呈现标题变量,可以为其余页面设置通用标题
<title> <%= locals.title ? title : 'Α generic title' %> </title>
是否在所有页面中呈现
标题
变量?如果不是,这就是为什么会出现未定义的错误。用locals
对象检查未定义的变量,并将变量包含在标题html标记中。如果不呈现标题变量,可以为其余页面设置通用标题
<title> <%= locals.title ? title : 'Α generic title' %> </title>
这个答案我已经用了很长时间了。但由于某些原因,当你共享链接时,它不起作用。它显示一个只有缩略图和网址的空预览。我已经使用这个答案很长时间了。但由于某些原因,当你共享链接时,它不起作用。它显示一个空预览,只有缩略图和网址。