首页 > 代码库 > 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