简体   繁体   English

将变量从javascript传递给ejs

[英]Pass a variable from javascript to ejs

I want to use a variable which is declared in javascript file to a ejs file. 我想使用在javascript文件中声明的变量到ejs文件。

javascript: JavaScript的:

var express = require('express');
var app = express();

var myVar = 1;

In the ejs file , where I want to use that variable inside a few if statements ,I have to declare it again in order to be able to use it. 在ejs文件中,我想在几个if语句中使用该变量,我必须再次声明它才能使用它。

ejs file: ejs文件:

var myVar = 1;
if ( my Var ) ....

How can I avoid this?Or is there a way for creating a configuration file which is accesible from both javascript and ejs? 我怎么能避免这种情况?或者有没有办法创建一个可以从javascript和ejs访问的配置文件?

I tried also to use: 我也试过用:

app.locals.myVar = 1

but it is undefined in the ejs file. 但它在ejs文件中未定义。

------- UPDATE -------------------------- -------更新--------------------------

In my code I am using: 在我的代码我正在使用:

app.get('/', function (req, res) {
    res.render('index', { user : req.user, message: [] });

});

after using the app.locals: 使用app.locals后:

app.get('/', function (req, res) {

   res.render('index', { user : req.user, message: [] });
   res.render('user_info.ejs');

}); });

and even though the code runs fine , I am receiving: 即使代码运行正常,我收到:

ReferenceError: ....user_info.ejs:18

 >> 18|            height:60px;


user is not defined
    at eval (eval at <anonymous> (...node_modules/ejs/lib/ejs.js:464:12), <anonymous>:20:12)

    at returnedFn (...node_modules/ejs/lib/ejs.js:493:17)
    at View.exports.renderFile [as engine] (.../node_modules/ejs/lib/ejs.js:350:31)
   .....

which doesn't make sence ,since as I said the code runs fine.And If I run it without the addition of the second res.render('user_info.ejs) I receive no errors. 因为我说代码运行正常。如果我在没有添加第二个res.render('user_info.ejs)情况下运行它,我没有收到任何错误。

So, can I have two res.render statements? 那么,我可以有两个res.render语句吗?

The app.locals.myVar approach should work, so something must be getting in the way. app.locals.myVar方法应该可行,所以必须要app.locals.myVar某些事情。 But you could avoid using app.locals.myVar altogether and pass variables directly to your views with: 但您可以完全避免使用app.locals.myVar 并将变量直接传递给您的视图:

var express = require('express');
var app = express();

app.get('/', function(req, res) {
    var myVar = 1;
    res.render('testPage', { myVar : myVar });
});

The myVar variable should now be available to the "testPage" ejs file. myVar变量现在应该可用于“testPage”ejs文件。 Inside of it you could do: 你可以在里面做:

<%= myVar %>

And see it output "1". 并看到它输出“1”。

Lastly, make sure you have set the view engine to ejs: 最后,确保已将视图引擎设置为ejs:

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

Otherwise it won't work. 否则它将无法工作。

Here's a very simple example on how app.locals can be used to expose variables to (EJS) templates: 这是一个关于app.locals如何用于向(EJS)模板公开变量的一个非常简单的例子:

// app.js
var express = require('express');
var app     = express();
var server  = app.listen(3000);

app.locals.myVar = 1;

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

// views/index.ejs
<% if (myVar) { %>
  <h1>myVar is here!</h1>
<% } else { %>
  <h1>Boohiss no myVar!</h1>
<% } %>

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

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