首页 > 解决方案 > 如何在将哈巴狗渲染成 HTML 时删除“>”

问题描述

我正在尝试制作日历。当我通过 JavaScript (Express) 将 pug 代码呈现为 HTML 时,html 代码涉及<and >(我什至根本没有输入),它会打印两次结果。我存储的值没有问题。我怎样才能解决这个问题?

以下是我的代码。

JavaScript:

var express = require('express');
var router = express.Router();
var moment = require('moment');
require('moment-timezone');
moment.tz.setDefault("Asia/Seoul");

//get the day of the first date of month
function getDay() {
  var date = new Date();
  var day = date.getDay();
  var _date = date.getDate();
  var firstDay = day - ((_date % 7) - 1);
  var month = date.getMonth();
  var dayPMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  var curMonth = dayPMonth[month];
  var year = date.getFullYear();
  var output = '';
  if(firstDay < 0) {
      firstDay = firstDay + 7;
  }
  for(var i = 0; i <= firstDay; i++) {
      output = output + '<li></li> '
  }

    if(month === 1) {
      if(year % 400 === 0) {
        curMonth++;
      }
      else if(year % 100 !== 0 && year % 4 === 0) {
        curMonth++;
      }
    }
    for(var i = 1; i <= curMonth; i++) {
        output = output + "<li class = \"date\">"+i;
        output = output + "</li> "
    }
    return output;
}

router.get('/', function(req, res, next) {
  var li = '';
  li = getDay();
  console.log(li);
  res.render('main', { title: 'ALMANAC', headerNum : 3, nickname : 'userId', notice: 'none', trophy : 0, money : 0, addLi : li});
});

module.exports = router;

哈巴狗:

 ol.days.list-unstyled
   #{addLi}

呈现的 HTML 代码:

在此处输入图像描述

标签: javascripthtmlpug

解决方案


这很容易通过哈巴狗each循环处理:

ol.days.list-unstyled
   each item in addLi
     li.date= item

只需将要显示的数字列表传递给它,然后将 HTML 生成留给 pug。


推荐阅读