artTemplate作为node服务端渲染引擎,怎么使用函数

渲染部分的代码

<h6>累计参与{{controlNumber(data.attendNum)}}人 奖励{{controlNumber(data.totalPrice)}}元</h6>    

controlNumber函数

/**
 * 数字处理函数
 * 每三个字符中间添加,
 */
const rgx = /(\d+)(\d{3})/;
let controlNumber = function(num){
    if(!num){
        return 0;
    }
    num = typeof num === 'number' ? num.toString() : num;
    if(num.length < 4){
        return num;
    }
    num += '';
    let x = num.split('.'),
        x1 = x[0],
        x2 = x[1];
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1,$2');
    }
    return x1 + (x2 ? x2.replace(/(\d{3})(?=[^$])/g,'$1,') : '');
};

export default controlNumber;

第一次使用模板引擎,感觉artTemplate比较好上手,不过现在用起来很局限

阅读 3.3k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题