首页 > 代码库 > lodash template filter
lodash template filter
<html> <head> <meta charset="UTF-8"> <title>template</title> <script src="lodash.js"></script> </head> <body> <script> var str = ‘的卡是d<%= name | prefix:"my-name" | sort: "asc" %>卡了‘.replace(/[\s]+/g, ‘‘).replace(/<%=(.+)%>/, function(exprs, path) { return exprs.replace(path, function(expr) { var ret = ‘(function() { var fns=(\‘‘+expr+‘\‘).split("|"); var data= http://www.mamicode.com/fns.shift();‘; /*ret += ‘fns.forEach(function(fn) {‘ +‘var fnInfo = fn.split(":");‘ +‘data = http://www.mamicode.com/fnInfo[0](data, fnInfo[1])‘>*/ ret += ‘ return _filter(‘+expr.split(‘|‘)[0]+‘, fns);})()‘; return ret; }); }); // console.log(str, ‘--------------------------------‘); var compiled = _.template(str, { imports: _.extend( _.templateSettings.imports, { prefix: function(d,s) { console.log(d); return ‘加了前缀了‘;}, sort: function(d, dir) { console.log(d);return ‘222222‘;}, _filter: function(data, filters) { var ret = data; var methods = _.templateSettings.imports; _.forEach(filters, function(filter) { var fnInfo = filter.split(":") ret = methods[fnInfo[0]].call(this, ret, fnInfo[1]); }, this); return ret; } }) })({ name: ‘zhangsan‘ }); console.log(compiled); </script> </body> </html>
lodash template filter
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。