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

Javascript模版引擎簡介

系統 2520 0

回顧

  • Micro-Templating

出自John Resig 2008年的一片文章,以及其經典實現:

    
      
        // 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生成代碼,終端動態編譯方案
  2. 使用with解決context問題
  • Mustache.js & othors

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

    
      /**
   * 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的編譯原理。

性能優化之路

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

  • 緩存技術

每次將Template字符串轉化成函數己經變成一種浪費,緩存簡單說是編譯后將函數cache起來,僅此而已。

  • context預賦值

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

    
      
        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);
    };
    
  
  • 規范關鍵字

作為最快的模版引擎,doT根本不使用with,而是直接通過規范關鍵字傳入參數為it,然后所有參數都用it的節點來引用。

  • 線下編譯

瀏覽器編譯過程轉為線下,直接生成執行函數。

  • 拼接方法

字符串拼接方法一般有:

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

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

未來

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

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

Javascript模版引擎簡介


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 成人精品视频一区二区在线 | 公主恋人在线观看 | 午夜精品亚洲 | 看免费5xxaaa | 久草视频精品在线 | 久久国产欧美另类久久久 | 日本人一级毛片免费视频 | 久久久久久日本一区99 | 亚洲女人逼 | 黄色成人一级片 | 国产精品四虎在线观看免费 | 日韩高清性爽一级毛片免费 | 青青青免费手机版视频在线观看 | 日日干日日操日日射 | 欧美 日产 国产精品 | 99视频精品全部国产盗摄视频 | 午夜二级 | 99re热久久精品这里都是精品 | 色拍自拍亚洲综合在线 | 成人欧美一区二区三区黑人3p | 香蕉在线精品亚洲第一区 | 国内精品自在自线视频香蕉 | 一级做受毛片免费大片 | sihu影院永久在线影院 | 天天干天天操天天做 | 毛片成人永久免费视频 | 亚洲 欧美 日韩 在线 香蕉 | 老司机伊人 | 久久精品国产99国产精品亚洲 | 欧美一级毛片aaaaa | 日韩精品中文字幕在线 | 欧美综合亚洲 | 97在线亚洲| 美女黄色一级毛片 | 国内精品综合九九久久精品 | 亚洲国产精品67194成人 | 一级a欧美毛片 | 欧美国产成人一区二区三区 | 色一情 | 黑人边吃奶边扎下面激情视频 | 国产精品久久久久久久网站 |