简体   繁体   English

动态设置元标签 EJS

[英]Dynamically set meta tags EJS

I am using EJS and I need to set meta tags for every post.我正在使用 EJS,我需要为每个帖子设置元标记。 I have boilerplate in the layouts folder, which I include on each page.我在 layouts 文件夹中有样板文件,我将其包含在每一页中。 When the user enters the posting page, I need to set dynamic meta tags and title.当用户进入发帖页面时,我需要设置动态元标签和标题。 My boilerplate我的样板

<!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>

I am trying to pass title to the posting page in this way我正在尝试以这种方式将标题传递给发布页面

res.render('post/index', {title: post.meta.title, post: post});

But I have an error that title is not defined in the boilerplate;但是我有一个错误,即样板中没有定义标题;

if i understood the problem correctly, i made a sample for you.如果我正确理解了这个问题,我为你做了一个样本。

mongoose schema for posts帖子的 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
})

Get the post data and rendered.获取帖子数据并渲染。

Post.findOne({ _id: req.params.id }, (err, data) => {
  res.render('post/index', { 'data': data })
}).lean()

and edit.ejs file.并编辑.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>

Do you render title variable in all pages?您是否在所有页面中呈现title变量? If not that's why you get undefined error.如果不是这就是为什么你得到未定义的错误。 Check for undefined with locals object and enclose your variable in title html tags.使用locals object 检查未定义并将您的变量包含在标题 html 标记中。
In case you don't render a title variable you can set a generic title for the rest of the pages如果您不呈现标题变量,您可以为页面的 rest 设置通用标题

<title> <%= locals.title ? title  : 'Α generic title' %> </title>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM