handlebars踩坑之旅

1. each的运用要领(遍历数组或许对象)

    {{include "tpl/components/page3-school-slide/each-slide"
      data='[
          { 
            "name": "badao", 
            "word": "刀"
          }, {
            "name": "changge",
            "word": "歌"
          }, {
            "name": "cangyun",
            "word": "云"
          }
      ]'
        
    }}
      

    {{#each data}}
      <div class="swiper-slide school-slide">
        {{include "tpl/components/page3-school-slide/index"
                  name=this.name
                  word=this.word
        }}
      </div>
    {{/each}}
    
    
    {{#each data}}
      <li class="{{../ns}}-tab-li"
      data-index={{@index}}     //数组遍用时索引值为 {{@index}}, 对象为{{@key}} 
      data-img="assets/images/page_exterior/tab/{{this.name}}.jpg" data-name="{{this.name}}">
        <img src="assets/images/page_exterior/tab/s/{{this.name}}.jpg" alt="">
     </li>
    {{/each}}

2. 推断是不是有传参, 有的话运用传参的值, 没有的话运用默认值


    {{#unless ns}}
        {{var ns = 'xxx'}}
    {{/unless}}

3. if else 推断

    {{#if list}}
    <ul id="list">  
        {{#each list}}
            <li>{{this.name}}</li>
        {{/each}}
    </ul>  
    {{else}}
        <p>{{error}}</p>
    {{/if}}
    
    
    
    //对应实用json数据
    var data = {  
        info:['HTML5','CSS3',"WebGL"],
        "error":"数据掏出毛病"
    }
    
    这里{{#if}}推断是不是存在list数组,假如存在则遍历list,假如不存在输出毛病信息

4. each 推断是不是是第一个或许是末了一个

    
    
    {{#each list}}
        ```
        {{#if @first}}  //{{#if @last}}
          ```
        {{else}}
         ```
        {{/if}}
        ```
    {{/each}}
    
    {{#each list}}
      <li class="page6-homepage-nav-li">
        <div class="page6-homepage-li-box">
          <div class="page6-homepage-playing-method-box">
            <div class="page6-homepage-method-img-box">
              <img src="assets/images/page6/homepage/img-{{this.method-type}}.png" alt="">
            </div>
            {{#if @first}}
              <div class="page6-homepage-method-nav-btn active" data-index="{{this.index}}">{{this.method-type-text}}</div>
            {{else}}
              <div class="page6-homepage-method-nav-btn" data-index="{{this.index}}">{{this.method-type-text}}</div>
            {{/if}}
            <p class="page6-homepage-method-introduce">{{this.method-type-introduce}}</p>
          </div>
        </div>
      </li>
    {{/each}}
  1. 遍用时给指定的某个值举行特别处置惩罚(运用比较要领)
{{#compare  name "==" "changge"}}
  do something
{{else}}
  do something else
{{/compare}}

{{#each data}}
  {{#compare this.num "==" 1}}
    <li class="tab-li active" data-img="assets/images/page_exterior/tab/{{this.num}}.jpg" data-name="{{this.name}}">
      <img src="assets/images/page_exterior/tab/s/{{this.num}}.jpg" alt="">
    </li>
  {{else}}
    <li class="tab-li" data-img="assets/images/page_exterior/tab/{{this.num}}.jpg" data-name="{{this.name}}">
      <img src="assets/images/page_exterior/tab/s/{{this.num}}.jpg" alt="">
    </li>
  {{/compare}}
{{/each}}

  1. content block 内容嵌套
// 外层
{{var ns = "layout-wrapper"}}
{{#extend "tpl/layouts/base-layout" }}
    {{#block "body"}}
      <!--页面正文从这里最先-->
    {{/block}}
{{/extend}}


// 内层:
{{#extend "tpl/layouts/index" title="题目"}}
  {{#content "body"}}
    {{var ns = "index"}}
    <style class="inline" type="text/less">

    </style>
    {{include "tpl/components/swiper/index"}}
    <div>插进去的页面内容</div> 
  {{/content}}
{{/extend}}
    原文作者:Xvolcano
    原文地址: https://segmentfault.com/a/1190000012971221
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞