最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

我如何获得Int Jade数据

网站源码admin20浏览0评论

我如何获得Int Jade数据

我如何获得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代码,用于获取数据#{}是正确的语法吗?

回答如下:

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

在pug specs (interpolation)中,您可以找到以下内容:

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

您可以像这样在模板中使用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

注意:由于计算了计数值,所以我没有对计数值进行转义(我使用了!{}语法)。

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论