最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

动态设置元标签EJS

IT培训 admin 11浏览 0评论

动态设置元标签EJS

我正在使用EJS,我需要为每个帖子设置元标记。我在layouts文件夹中有样板,我将其包含在每个页面中。当用户进入帖子页面时,我需要设置动态元标记和标题。我的样板

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <%= 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=".11.2/css/all.css">
    <link rel="stylesheet" href=".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>

我正试图通过这种方式将标题传递给帖子页面

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

但是我有一个错误,即标题没有在样板中定义;

回答如下:

如果我正确理解了问题,我为您制作了一个样本。

帖子的猫鼬模式

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

动态设置元标签EJS

我正在使用EJS,我需要为每个帖子设置元标记。我在layouts文件夹中有样板,我将其包含在每个页面中。当用户进入帖子页面时,我需要设置动态元标记和标题。我的样板

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <%= 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=".11.2/css/all.css">
    <link rel="stylesheet" href=".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>

我正试图通过这种方式将标题传递给帖子页面

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

但是我有一个错误,即标题没有在样板中定义;

回答如下:

如果我正确理解了问题,我为您制作了一个样本。

帖子的猫鼬模式

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

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论