我是靠谱客的博主 积极太阳,这篇文章主要介绍简易的js模板工具,现在分享给大家,希望可以做个参考。

简单的js模板工具1,从Extjs中撮出来的。


var re=/{([w-]+)(?::([w.]*)(?:((.*?)?))?)?}/g;
String.prototype.tpl=function(obj){
return this.replace(re,function(m, name){
return obj[name] !== undefined ? obj[name] : "";
});
};

var tpl='<div>{a}</div><br/><div>{b}</div>';
console.log( tpl.tpl({a:'mfk',b:'vfn'}) ); //<div>mfk</div><br/><div>vfn</div>
console.log( tpl.tpl({a:'it',b:'eye'}) ); //<div>it</div><br/><div>eye</div>



简单的js模板工具2,仿Java的String.format()。

String.prototype.format=function(){
var i=0,args=arguments;
return this.replace(/%s/g,function(){
var v=args[i++];
return v !== undefined ? v : '';
});
};

var tpl='first:%s,last:%s';
console.log( tpl.format('mfk','vfn') ); //first:mfk,last:vfn
console.log( tpl.format('it') ); //first:it,last:

最后

以上就是积极太阳最近收集整理的关于简易的js模板工具的全部内容,更多相关简易内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(130)

评论列表共有 0 条评论

立即
投稿
返回
顶部