我如何获得Data int Jade

给了

我做了列表页面分页。我想列出一些分页号码清单。但我不知道如何在玉器中获取数据。

-for(var i=0; i < #{totalCnt}; i++){
                 form(action='/topic')
                     input(type='button' name='nowPage' value=i) 
             -}

这是我的Jade代码#{totalCnt}不起作用

app.get("/topic", function(req, res) {
  var nowPage = req.query.nowPage;
  //전체 리스트를 띄우는 기본 리스트 화면
  var sqlCnt = "select count(*) from topic";
  var sql = "SELECT id, title FROM topic ORDER BY id";
  client.query(sqlCnt, function(err, res3) {
    client.query(sql, function(err, res2) {
      if (err) {
        console.log(err);
      } else {
        if (nowPage == null || nowPage == "" || nowPage <= 1) nowPage = 1;
        var totalCnt = res3.rows;
        res.render("view", {
          topics: res2.rows,
          totalCnt: totalCnt,
          nowPage: nowPage
        });
      }
    });
  });
});

这是我的javascript代码获取数据#{}是正确的语法吗?

马克西米利安·菲塞尔(Maximilian Fixl)

您已经将带有res.render函数的变量传递view文件。确保将视图文件命名为view.pug您将.pug文件名作为第一个参数传递"view"

哈巴狗规格(插值)中,您可以找到以下内容:

如果您需要包含逐字##,则可以将其转义,或使用插值。

您可以像这样在模板中使用render函数中的变量:

app.js

app.get("/topic", function(req, res) {
    var nowPage = req.query.nowPage;

    client.query("select id, title from topic order by id", (err, result) => {

        if (!nowPage || nowPage === 1)
            nowPage = 1;
                
        res.render("view", {
            title: 'Topics',
            topics: result.rows,
            totalCnt: result.rowCount,
            nowPage: nowPage,
            errors: err
        });
    });
});

view.pug

h1 #{title}
p There are !{totalCnt} topics on !{nowPage} page

if topics
  ul
    for topic in topics
      li ID: #{topic.id}, Title:  #{topic.title}
else if errors
  ul
    for error in errors
      li!= error.msg

注意:!{}因为计数值是经过计算的,所以我没有对计数值进行转义(我使用了语法)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章