EJS - 包括来自 JSON 文件的配置
EJS - Include config from JSON File
如何将值从 .json
文件加载到 .ejs
文件。
示例文件.json
{
"value": "value2"
}
如何从 value
列加载值 value2
?
示例输出(HTML 由 EJS 呈现)
Yes, that's "value2"
//read the json file
const values = require("./pathtojson/file.json");
app.locals
可用于将变量公开给 (EJS) 模板,例如:
// app.js
var express = require('express');
var app = express();
var server = app.listen(3000);
//using app.locals
app.get('/', function(req, res) {
app.locals.values = 1;
res.render('index.ejs');
});
//another way of passing variables is:
app.get('/anotherway', function(req, res) {
res.render('testPage', { jsonValue: values });
});
如何将值从 .json
文件加载到 .ejs
文件。
示例文件.json
{
"value": "value2"
}
如何从 value
列加载值 value2
?
示例输出(HTML 由 EJS 呈现)
Yes, that's "value2"
//read the json file
const values = require("./pathtojson/file.json");
app.locals
可用于将变量公开给 (EJS) 模板,例如:
// app.js
var express = require('express');
var app = express();
var server = app.listen(3000);
//using app.locals
app.get('/', function(req, res) {
app.locals.values = 1;
res.render('index.ejs');
});
//another way of passing variables is:
app.get('/anotherway', function(req, res) {
res.render('testPage', { jsonValue: values });
});