nodeJS如何使用路由器将图标标签添加到pug文件中?



您好,我认为显示部分代码行更容易。
我正在尝试做的是当我输入邮政编码时,将显示正确的图标。 我正在使用这个 git https://erikflowers.github.io/weather-icons/。
例如:如果纽约天气状况晴朗 天气中的天气状况应该像i.wi.wi-night-sleet是否可以在主题.js的图标标签中添加类名?
或 我可以像- if text=='clear' i.wi.wi-night-sleet一样在哈巴狗蝇中使用相等的语句吗

主题.js

router.post('/weather', function(req,res){
let url = `http://api.openweathermap.org/data/2.5/weather?zip=${req.body.zipcode}&units=imperial&appid=${apiKey}`
request(url, function (err, response, body) {
if(err){
res.status(500).send('Internal Server Error');
console.log('error: ' ,err);
} else {
if(req.body.zipcode.length != 5) {
res.render('topic/weather', {text: "Zipcode does not exist"})
} else {
let weather = JSON.parse(body)
let temp = weather.main.temp
let location = weather.name;
let day_weather = weather.weather[0].main;
let message = `It's ${weather.main.temp} degrees in ${weather.name}!`;

//below this I want to call icon tag that has a class name
res.render('topic/weather', {text: location + " : " + day_weather, weatherCondition: `i.wi.wi-night-sleet`});
}
}
});
})

天气.哈巴狗

extends ./homepage
block navigate
div.container-fluid
div.row.row-centered
p= text
//- space 넣을떄
= "  "
if text
= date

div.col-lg-6.col-centered
form.form-group(action='/weather', method='post')
p    
input(type='text', class="form-control", id="zipcode",name='zipcode', placeholder='zipcode')
p
button(type='submit', class="btn btn-primary btn-lg" style='margin-right: 10px;') Login 

在您的路线中,只需传递您需要的图标的识别部分:

res.render('topic/weather', {text: location + " : " + day_weather, weatherCondition: "night-sleet"});

然后这是你的哈巴狗模板需要的样子:

i.wi(class= 'wi-' + weatherCondition)

i(class= 'wi wi-' + weatherCondition)

这些哈巴狗行中的任何一行都会产生相同的 html:

<i class="wi wi-night-sleet"></i>

最新更新