lodash template filter
Posted 风之约
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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= fns.shift();‘; /*ret += ‘fns.forEach(function(fn) {‘ +‘var fnInfo = fn.split(":");‘ +‘data = 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的主要内容,如果未能解决你的问题,请参考以下文章
/node_modules/lodash.template/index.js:1558 throw result; ^ SyntaxError: Unexpected token = 报错解决(代码