亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Javascript模版引擎簡介

系統(tǒng) 2980 0

回顧

  • Micro-Templating

出自John Resig 2008年的一片文章,以及其經(jīng)典實(shí)現(xiàn):

    
      
        // Simple JavaScript Templating
      
      
        // John Resig - http://ejohn.org/ - MIT Licensed
      
      
(
      
        function
      
      (){
  
      
        var
      
       cache = {};

  
      
        this
      
      .tmpl = 
      
        
          function
        
        
          tmpl
        
        
          (str, data)
        
        {
      
      
        // Figure out if we're getting a template, or if we need to
      
      
        // load the template - and be sure to cache the result.
      
      
        var
      
       fn = !
      
        /\W/
      
      .test(str) ?
      cache[str] = cache[str] ||
        tmpl(document.getElementById(str).innerHTML) :

      
      
        // Generate a reusable function that will serve as a template
      
      
        // generator (and which will be cached).
      
      
        new
      
       Function(
      
        "obj"
      
      ,
        
      
        "var p=[],print=function(){p.push.apply(p,arguments);};"
      
       +

        
      
        // Introduce the data as local variables using with(){}
      
      
        "with(obj){p.push('"
      
       +

        
      
        // Convert the template into pure JavaScript
      
      
        str
          .replace(
      
        /[\r\t\n]/g
      
      , 
      
        " "
      
      )
          .split(
      
        "<%"
      
      ).join(
      
        "\t"
      
      )
          .replace(
      
        /((^|%>)[^\t]*)'/g
      
      , 
      
        "$1\r"
      
      )
          .replace(
      
        /\t=(.*?)%>/g
      
      , 
      
        "',$1,'"
      
      )
          .split(
      
        "\t"
      
      ).join(
      
        "');"
      
      )
          .split(
      
        "%>"
      
      ).join(
      
        "p.push('"
      
      )
          .split(
      
        "\r"
      
      ).join(
      
        "\\'"
      
      )
      + 
      
        "');}return p.join('');"
      
      );

    
      
        // Provide some basic currying to the user
      
      
        return
      
       data ? fn( data ) : fn;
  };
})();
    
  
  1. 基本的replace生成代碼,終端動態(tài)編譯方案
  2. 使用with解決context問題
  • Mustache.js & othors

更加豐富的模版語法,以及更加容易擴(kuò)展。

    
      /**
   * Breaks up the given `template` string into a tree of tokens. If the `tags`
   * argument 
      
        is
      
       given here it must be an array 
      
        with
      
       two string values: the
   * opening 
      
        and
      
       closing tags used 
      
        in
      
       the template (e.g. [ 
      
        "<%"
      
      , 
      
        "%>"
      
       ]). Of
   * course, the default 
      
        is
      
       to use mustaches (i.e. mustache.tags).
   *
   * A token 
      
        is
      
       an array 
      
        with
      
       at least 
      
        4
      
       elements. The first element 
      
        is
      
       the
   * mustache symbol that was used inside the tag, e.g. 
      
        "#"
      
      
        or
      
      
        "&"
      
      . If the tag
   * did 
      
        not
      
       contain a symbol (i.e. {{myValue}}) this element 
      
        is
      
      
        "name"
      
      . For
   * all text that appears outside a symbol this element 
      
        is
      
      
        "text"
      
      .
   *
   * The second element of a token 
      
        is
      
       its 
      
        "value"
      
      . For mustache tags this 
      
        is
      
      
   * whatever 
      
        else
      
       was inside the tag besides the opening symbol. For text tokens
   * this 
      
        is
      
       the text itself.
   *
   * The third 
      
        and
      
       fourth elements of the token are the start 
      
        and
      
       end indices,
   * respectively, of the token 
      
        in
      
       the original template.
   *
   * Tokens that are the root node of a subtree contain two more elements: 
      
        1
      
      ) an
   * array of tokens 
      
        in
      
       the subtree 
      
        and
      
      
        2
      
      ) the index 
      
        in
      
       the original template at
   * which the closing tag 
      
        for
      
       that section begins.
   */
    
  

我們可以從這段備注中簡單的看出Mustache.js的編譯原理。

性能優(yōu)化之路

模版引擎成功將動態(tài)HTML代碼從Javascript中分離出來,避免了從前頻繁的Javascript代碼中的字符串拼接,簡化了編碼工作,實(shí)則是前端發(fā)展的大躍進(jìn)。但當(dāng)部分人還在癡迷與模版引擎的功能時,已經(jīng)有人朝性能方向邁進(jìn)。

  • 緩存技術(shù)

每次將Template字符串轉(zhuǎn)化成函數(shù)己經(jīng)變成一種浪費(fèi),緩存簡單說是編譯后將函數(shù)cache起來,僅此而已。

  • context預(yù)賦值

為了避免使用with這種效率較低的方法而出現(xiàn)的,簡單的說就是把傳入的數(shù)據(jù)對象中的所有節(jié)點(diǎn)都變成局部變量,下面是一個簡單的例子:

    
      
        var
      
       compile = 
      
        function
      
      (str){
        
      
        //避免with語法
      
      
        var
      
       strFn = 
      
        "var _$jstpl='',__fn__=(function(__d__){var __v__='';for(var __k__ in __d__){__v__+=('var '+__k__+'=__d__[\"'+__k__+'\"];');};eval(__v__);_$jstpl+='"
      
       + parse(str) + 
      
        "';__v__=null;})(param);__fn__ = null;return _$jstpl;"
      
      ;
        
      
        return
      
      
        new
      
       Function(
      
        "param"
      
      , strFn);
    };
    
  
  • 規(guī)范關(guān)鍵字

作為最快的模版引擎,doT根本不使用with,而是直接通過規(guī)范關(guān)鍵字傳入?yún)?shù)為it,然后所有參數(shù)都用it的節(jié)點(diǎn)來引用。

  • 線下編譯

瀏覽器編譯過程轉(zhuǎn)為線下,直接生成執(zhí)行函數(shù)。

  • 拼接方法

字符串拼接方法一般有:

  1. arr.push & arr.join
  2. res += tmp
  3. res = res + tmp

很多人誤以為數(shù)組 push 方法拼接字符串會比 += 快,要知道這僅僅是 IE6-8 的瀏覽器下。實(shí)測表明現(xiàn)代瀏覽器使用 += 會比數(shù)組 push 方法快,而在 v8 引擎中,使用 += 方式比數(shù)組拼接快 4.7 倍。最新的一些測試結(jié)果還發(fā)現(xiàn) res = res + tmp 在v8某些版本甚至比 res += tmp 還快。

未來

有些時候流行總在輪回,比如黑框眼睛以前是我們奶奶那輩人戴的,但現(xiàn)在年輕人都開始戴了。

模版從后端render,變成前端render,變成線下render……現(xiàn)在又隨著NodeJS的崛起回來了后端(前臺?)render,部分大公司如: Facebook Google 已經(jīng)線上應(yīng)用。

Javascript模版引擎簡介


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久久久免费 | 狠狠色噜噜狠狠狠米奇9999 | 国产精品久久久视频 | 国产91成人| 日本免费视屏 | 毛片网站免费在线观看 | 狠狠色丁香婷综合久久 | 日本久久久久久久中文字幕 | 久久精品在线播放 | 日韩欧美中文字幕一区二区三区 | 欧美高清不卡 | 国产一级毛片夜一级毛片 | 成人sese| 欧美日本免费观看αv片 | 99这里都是精品 | 亚洲狠狠操 | 国产精品第4页 | 精品久久久久久亚洲精品 | 四虎www. | 91国内在线观看 | 亚洲精品乱码蜜桃久久久 | 91成人免费观看网站 | 国产免费高清视频 | 四虎国产精品免费入口 | 免费女人18a级毛片视频 | 久久精品国产日本波多麻结衣 | 中文字幕亚洲综久久2021 | 精品国产免费一区二区三区 | 欧美激情区 | 国产在线欧美日韩一区二区 | 麻豆久久婷婷综合五月国产 | 中国精品白嫩bbwbbw | 午夜成人免费影院 | 99爱国产| 免费观看一级特黄欧美大片 | 四虎影视网址 | 亚洲你xx我xx网站 | 在线欧美精品国产综合五月 | 亚洲欧美国产精品久久久 | 尤物在线视频 | 久久精品国产曰本波多野结衣 |