• 前端模板引擎 超轻量


    ;(function(global ,undefined){
        var cache = {}
        var sn = '_ret_'  
        function etic(tplId , data){
            if (!tplId) return
            var tplNode  = document.getElementById(tplId)
            if (!tplNode) return
            var organ = tplNode.getAttribute('organ')
    
            function  cbk(){
                return data ? cache[tplId](data) : cache[tplId]
            }
    
            if (cache[tplId]) return cbk()
    
            var tpl = tplNode.innerHTML
            var con = 'var ' + sn + ' ="" ;'
            function tLine(str ){
                return str.replace(/[
    	
    ]/g, " ").replace(/'/g , "\'")
            }
    
            while (true){
                var sPos = tpl.indexOf('<?')
                if (-1 == sPos) break
                var ePos = tpl.indexOf('?>' , sPos + 2)
    
                var part1 = tpl.slice(0,sPos)
                    , f = tpl.slice(sPos + 2 , ePos)
                    ,tpl = tpl.slice(ePos + 2)
                var op = f.charAt(0)
                if (part1.length) con += sn + " += '" + tLine(part1) + "';"
                switch (op){
                    case '=' :
                        f = f.slice(1)
                        con += sn + " += " + f + ";"
                        break
                    case '#' :
                        f = f.slice(1).replace('./' , organ + '/').replace('/','_').replace('.html' , '').replace(/ /g,'')
                        con += sn +  " += etic('" + f + "')(this);"
                        break
                    default:
                        con += f
                }
    
            }
    
            tpl.length  && (con += sn + " += '" + tLine(tpl) + "';")
            con += 'return ' + sn
            try{
                var t = new Function("" , con)
                cache[tplId] = function (data){ return t.call(data)}
                return cbk()
            }catch(e){
                console && console.log(e , tpl)
            }
    
        }
        global.etic = etic
    })(this)
  • 相关阅读:
    noip2012 同余方程
    bzoj1477 poj1061 青蛙的约会
    Nginx 从入门到放弃(五)
    Nginx 从入门到放弃(四)
    Nginx 从入门到放弃(三)
    Nginx 从入门到放弃(二)
    Nginx 从入门到放弃(一)
    python三大神器之fabric
    Docker 学习(一)
    linux就该这么学 第一天学习笔记
  • 原文地址:https://www.cnblogs.com/vaal-water/p/3925124.html
Copyright © 2020-2023  润新知