【发布时间】:2017-05-08 01:35:18
【问题描述】:
我花了将近四个小时都无法解决这个问题,而且我找不到任何有用的文档来解决这类问题。这就是问题所在,我正在使用 pug/jade 模板,我想在 pug 模板中调用函数来转换一些数据 这是主要模板:
/** main template */
section
each pet in pets
.pet
.photo-column
img(src= pet.photo)
.info-column
h2= pet.name
span.species= (pet.species)
p Age: #{calculateAge(pet.birthYear)} //here I need to call calculateAge function
if pet.favFoods
h4.headline-bar Favorite Foods
ul.favorite-foods
each favFood in pet.favFoods
li!= favFood
/** end main template **/
这是外部函数:
/** calculateAge.js **/
module.exports = function(birthYear) {
var age = new Date().getFullYear() - birthYear;
if (age > 0) {
return age + " years old";
} else {
return "Less than a year old";
}
};
/** end calculateAge.js **/
我做了什么外壳来实现这一点?
【问题讨论】:
标签: javascript templates pug