获取ejs模板中的url参数

时间:2022-10-05 20:29:53

I am trying to create an ejs conditional based on a URL parameter, for example, if the test parameter exists at localhost:3000/page?test, then show a div, else dont show it.

我试图基于URL参数创建一个ejs条件,例如,如果测试参数存在于localhost:3000 / page?test,那么显示一个div,否则不显示它。

My ejs template looks something like:

我的ejs模板看起来像:

<% include header %>
<div class="row">
<%if (title !== "homepage") {%>
<%= title %>
  <div>
    <% include nav %>
  </div>
<%}%>
  <div>
  </div>
</div>
<% include footer %>

Is there a way to access the URL parameters directly from the ejs file? For example, the <%= title %> works fine, is there something like <%= req.query %>?

有没有办法直接从ejs文件访问URL参数?例如,<%= title%>工作正常,有类似<%= req.query%>的东西吗?

I am also using express.

我也在使用快递。

2 个解决方案

#1


12  

You can pass it easily as an object in the second argument to render()

您可以轻松地将其作为第二个参数中的对象传递给render()

app.get('/someurl', function(req, res, next) {
   res.render('filename', {query : req.query});
});

You can also use the locals variable

您还可以使用locals变量

app.get('/someurl', function(req, res, next) {
   res.locals.query = req.query;
   res.render('filename');
});

which is very useful when used with a general route that runs before all the other routes, making the variable available in all the following routes

当与在所有其他路由之前运行的常规路由一起使用时,这非常有用,使得变量在以下所有路由中都可用

app.use(function(req, res, next) {
   res.locals.query = req.query;
   res.locals.url   = req.originalUrl;

   next();
});

and it's available in the file you're rendering as query etc

并且它在您作为查询等呈现的文件中可用

<% if (query == "something") { %>
    <div id="crazy_shit">
        <a href="<%- url -%>">Here</a>
    </div>
<% } %>

As a sidenote, if query for some reason isn't defined, you'll get an error in EJS for using an undefined variable, which can be annoying.

作为旁注,如果由于某种原因未定义查询,则在EJS中使用未定义变量时会出现错误,这可能很烦人。

I usually solve this by using an object instead, as checking object properties does not trigger errors, and it's easy to make sure the object always has an initial value at the top of each EJS template.
It's done like this in the routes

我通常使用对象来解决这个问题,因为检查对象属性不会触发错误,并且很容易确保对象始终在每个EJS模板的顶部都有一个初始值。这是在路线中这样做的

app.user(function(req, res, next) {
   res.locals.stuff = {
       query : req.query,
       url   : req.originalUrl
   }

   next();
});

And then in the template

然后在模板中

<% stuff = typeof stuff !== 'object' ? {} : stuff %>

// later on

<% if ( stuff.query == "something") { %>//does not throw error if property not defined
    <div id="crazy_shit"></div>         
<% } %>

which even if stuff.query is defined, the condition just fail and it doesn't throw an error like it would if stuff itself, or any other variable, wasn't defined.

即使定义了stuff.query,条件也会失败,并且它不会像没有定义东西本身或任何其他变量那样抛出错误。

#2


5  

<%= req.query.paramName %>

Works for me where paramName is the name of your URL query parameter.

适用于我,其中paramName是您的URL查询参数的名称。

#1


12  

You can pass it easily as an object in the second argument to render()

您可以轻松地将其作为第二个参数中的对象传递给render()

app.get('/someurl', function(req, res, next) {
   res.render('filename', {query : req.query});
});

You can also use the locals variable

您还可以使用locals变量

app.get('/someurl', function(req, res, next) {
   res.locals.query = req.query;
   res.render('filename');
});

which is very useful when used with a general route that runs before all the other routes, making the variable available in all the following routes

当与在所有其他路由之前运行的常规路由一起使用时,这非常有用,使得变量在以下所有路由中都可用

app.use(function(req, res, next) {
   res.locals.query = req.query;
   res.locals.url   = req.originalUrl;

   next();
});

and it's available in the file you're rendering as query etc

并且它在您作为查询等呈现的文件中可用

<% if (query == "something") { %>
    <div id="crazy_shit">
        <a href="<%- url -%>">Here</a>
    </div>
<% } %>

As a sidenote, if query for some reason isn't defined, you'll get an error in EJS for using an undefined variable, which can be annoying.

作为旁注,如果由于某种原因未定义查询,则在EJS中使用未定义变量时会出现错误,这可能很烦人。

I usually solve this by using an object instead, as checking object properties does not trigger errors, and it's easy to make sure the object always has an initial value at the top of each EJS template.
It's done like this in the routes

我通常使用对象来解决这个问题,因为检查对象属性不会触发错误,并且很容易确保对象始终在每个EJS模板的顶部都有一个初始值。这是在路线中这样做的

app.user(function(req, res, next) {
   res.locals.stuff = {
       query : req.query,
       url   : req.originalUrl
   }

   next();
});

And then in the template

然后在模板中

<% stuff = typeof stuff !== 'object' ? {} : stuff %>

// later on

<% if ( stuff.query == "something") { %>//does not throw error if property not defined
    <div id="crazy_shit"></div>         
<% } %>

which even if stuff.query is defined, the condition just fail and it doesn't throw an error like it would if stuff itself, or any other variable, wasn't defined.

即使定义了stuff.query,条件也会失败,并且它不会像没有定义东西本身或任何其他变量那样抛出错误。

#2


5  

<%= req.query.paramName %>

Works for me where paramName is the name of your URL query parameter.

适用于我,其中paramName是您的URL查询参数的名称。