如何在 ejs 模板中使用 URL 发送值

How to send value with URL in ejs template

我想发送一个我从数据库中获取的值

<% pp.forEach(function(a){ %>
   <h1><a href="/users/gotospecific/{a.title}"><%= a.title %></a></h1>

但由于一些问题发生了错误。

有人可以指导我解决这个问题吗?

ejs 的正确语法是:

<% pp.forEach(function(a){ %>
  <h1><a href="/users/gotospecific/"+<%= a.title %>+""><%= a.title %></a></h1>
<% }); %>

如果您想在您的服务器上使用以下内容:

app.get('/gotospecific', function(req, res) {
    var topic = req.query.topic;
    ...
})

那么您将需要在 EJS 中使用:

<% pp.forEach(function(a) { %>
    <h1><a href="/users/gotospecific?topic=<%= a.title %>"><%= a.title %></a></h1>
<% }); %>

其中 pp 被假定为具有 title 属性的对象数组。例如

res.render('myview', {pp: [{title: 'First item'}]});