使用ejs在Node.js中传递局部变量-文件

koaltpgm  于 2023-05-17  发布在  Node.js
关注(0)|答案(2)|浏览(157)

我想在Node.js中创建一个测试项目。
我有一个索引.ejs

<!DOCTYPE html>
<html>
  <head>
    <title>Meine Node.js-Testanwendung</title>
  </head>
  <body>
    <h1>Willkommen auf dem Mars!</h1>
    <%- include("locvar.ejs"); %>
    <%- include("result.ejs"); %>
  </body>
</html>

localvar.ejs

<%
  var var1 = "Mars";
  var var2 = "Hallo";
%>

结果.ejs

<%- include("locvar.ejs"); %>
<h2>Original Variablen:</h2>
<p>var1: <%= var1 %></p>
<p>var2: <%= var2 %></p>

<%- include ("change.ejs"); %>
<h2>Change Variablen:</h2>
<p>var1: <%= var1 %></p>
<p>var2: <%= var2 %></p>

<%
  var1 = "Houston";
  var2 = "Problem";
%>

<h2>Geänderte Variablen:</h2>
<p>var1: <%= var1 %></p>
<p>var2: <%= var2 %></p>

和改变.ejs

<%
  var1 = "Hallo";
  var2 = "Mond";
%>

这里是server.js

const express = require('express');
const app = express();
const ejs = require('ejs');

app.set('view engine', 'ejs');

app.get('/', (req, res) => {
  res.render('index.ejs');
});

app.listen(3000, () => {
  console.log('Server started on port 3000');
});

如何传递局部变量?
首先,result.ejs应该首先显示来自locvar.ejs的数据,然后显示来自change.ejs的数据,最后显示来自result的数据。

6kkfgxo0

6kkfgxo01#

你可以使用locals对象。在本例中,您希望将locvar.ejs中定义的变量传递给result.ejs
index.ejs应该包含result.js

<!DOCTYPE html>
<html>
  <head>
    <title>Meine Node.js-Testanwendung</title>
  </head>
  <body>
    <h1>Willkommen auf dem Mars!</h1>
    <% var var1 = "Mars"; %>
    <% var var2 = "Hallo"; %>
    <% include("result.ejs", { var1: var1, var2: var2 }); %>
  </body>
</html>

修改result.ejs以显示从index.ejs传递的变量,然后呈现change.ejs

<%- include("locvar.ejs"); %>
<h2>Original Variablen:</h2>
<p>var1: <%= locals.var1 %></p>
<p>var2: <%= locals.var2 %></p>

<%- include ("change.ejs", { var1: locals.var1, var2: locals.var2 }); %>

<h2>Geänderte Variablen:</h2>
<p>var1: <%= locals.var1 %></p>
<p>var2: <%= locals.var2 %></p>

修改change.ejs以显示从result.ejs传递的变量

<h2>Change Variablen:</h2>
<p>var1: <%= locals.var1 %></p>
<p>var2: <%= locals.var2 %></p>

<%
  locals.var1 = "Hallo";
  locals.var2 = "Mond";
%>
mwkjh3gx

mwkjh3gx2#

这就是解决方案:
server.js:

const express = require('express');
const session = require('express-session');
const ejs = require('ejs');
const path = require('path');
const local = require('./locvar');

const app = express();

// Middleware to initialize the session object
app.use(session({
  secret: 'my-secret-key',
  resave: false,
  saveUninitialized: true
}));

// Middleware to expose the config object to all routes
app.use((req, res, next) => {
  res.locals.local = local;
  next();
});

// Set up the view engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// Route to render the index.ejs template
app.get('/', (req, res) => {
  res.render('index');
});

// Start the server
app.listen(3000, () => {
  console.log('Server started on port 3000');
});

locvar.js <- nicht .ejs

<%
  var var1 = "Mars";
  var var2 = "Hallo";
%>

index.ejs

<!DOCTYPE html>
<html> 
  <head>
    <title>Meine Node.js-Testanwendung</title>
  </head>
  <body>
    <h1>Willkommen auf dem Mars!</h1>
    <%- include("result.ejs"); %>
  </body>
</html>

result.ejs

<h1>Original Variablen </h1>
<p>var1: <%= local.var1 %></p>
<p>var2: <%= local.var2 %></p>

<%- include("change.ejs"); %>
<h1>Change Variablen </h1>
<p>var1: <%= local.var1 %></p>
<p>var2: <%= local.var2 %></p>

<%
  local.var1 = "Housten";
  local.var2 = "Problem";
%>
<h1>Result Variablen </h1>
<p>var1: <%= local.var1 %></p>
<p>var2: <%= local.var2 %></p>e

change.ejs

<%
  local.var1 = "Hallo";
  local.var2 = "Mond";
%>

重要的是,我们在server.js中有middelware-route

相关问题