mustache.js 使用

來源:互聯網
上載者:User

標籤:對象數組   表資料   文法   type   根據   bsp   box   ...   cti   

  對於mustache模板,我是屬於即用即查的方法,以下記錄僅是我常用的方式。方便以後使用時不用再去項目中去找,因為真的不好找。(此處 -->serious 臉)

  當需要渲染一些資料列表的時候,使用這個模板是比較快捷的。(具體詳情文法看文檔)

   一.首先需要添加html模板部分。

   <script id="template" type="x-tmpl-mustache">

    {{#data}}   -->列表資料 

       //此處只是樣本,換成需要迴圈的列表即可 

      <h3 class="order-num-box">訂單號:<span class="order-num">{{payid}}(此處寫資料迴圈列表中的變數名,為了綁定欄位中的內容不需要被轉義我們可以{{&payid}})</span> 

      <span class="item-time pull-right">下單時間:{{paytime}}</span>

    </h3> 

    {{/data}}
  </script>

   二. 擷取模板

       var tmpl = $(‘#template‘).html();

  三. 渲染模板

    function showRecordData(data) {

       Mustache.parse(tmpl);

      var rendered = Mustache.render(tmpl, {
      dataRecord: data,  //資料來源   --對象數組
      render: function() {  //如果有需要根據不同狀態或者類型返回不同的值或者html片段時,可以通過函數來實現
      if (this.type == 類型值) {
          return "...." ;
        
      } else {
         return "...";

    }
  }
  });
  return rendered;  //返回渲染好的html資料。 
}

 

mustache.js 使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.