我想将 Head 和 Body 合并到车把中(node.js)

Posted

技术标签:

【中文标题】我想将 Head 和 Body 合并到车把中(node.js)【英文标题】:i want to incorporate Head and Body in handlebars (node.js) 【发布时间】:2021-11-02 13:07:08 【问题描述】:

我想把我的头和身体放在车把上,但我不知道怎么做,

我将举例说明我如何使用它'''

<link rel="stylesheet" href="/css/index.css">
<h1> test</h1>

''' 车把'''

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/css/nome.css">
    <title>Documen</title>
</head>
<body>
    body
</body>
</html>

'''

通过这段代码,我将每个元素都传递到这个 body 对吗? 我也想通过一个人头,我该怎么做?

如果问题混淆了真的很抱歉,我真的不知道如何更好地解释

【问题讨论】:

这些答案解决了你的问题吗? 【参考方案1】:

首先你需要安装npm install express express-handlebars。 这里有文件夹和文件项目结构:


app.js

const express = require("express");
const exphbs = require("express-handlebars");
const app = express();
const PORT = process.env.PORT || 3000;

// view engine setup
app.engine(
  "hbs",
  exphbs.engine(
    defaultLayout: "main",
    extname: ".hbs",
  )
);

app.set("view engine", "hbs");

app.get("/", (req, res) => 
  res.render("home");
);

app.get("/about", (req, res) => 
  res.render("about");
);

app.listen(PORT, () => 
  console.log(`Node Server running on port: http://localhost:$PORT`);
);

main.hbs

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    <title>Hello, world!</title>
</head>

<body>
    >header

    <div class="container d-flex justify-content-center align-items-center flex-column" style="height: 800px;">
        body
        >mainContent
    </div>


    >footer
    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
        crossorigin="anonymous"></script>
</body>

</html>

header.hbs

<header>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">Navbar</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
                aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link active" aria-current="page" href="/">Home</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/about">about</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
</header>

mainContent.hbs

<h2>Hello World from Handlebars</h2>
<h3>Express</h3>
<h4>Bootstrap 5</h4>

页脚.hbs

<footer>
    <p class="text-center text-muted">&copy; Copyright 2021 The Awesome Stuff</p>
</footer>

home.hbs

<h1>Home Page</h1>

关于.hbs

<h1>About Page</h1>

输出:

"express": "^4.17.1","express-handlebars": "^6.0.2"

【讨论】:

以上是关于我想将 Head 和 Body 合并到车把中(node.js)的主要内容,如果未能解决你的问题,请参考以下文章

如何在车把文件中打印“body”元素?

在 Mandrill 车把合并变量中使用 HTML

车把,访问父每个循环变量

如果我想将分支合并到主干上,“svn merge --reintegrate”和 svn merge without reintegrate 有啥区别

水平居中显示 iFrame

尝试从 Mandrill 发送电子邮件时出现 html 或车把编码问题