当前位置:   article > 正文

swift 之 mustache模板引擎

swiftmustache

用法:

  • Variable Tags

    1. {{name}} 用来渲染值name

      1. datas:
      2. let data = ["value": "test"]
      3. -----------
      4. template:
      5. {{value}} a lession
      6. -----------
      7. rendering:
      8. test a lession
  • Section Tags

    1. {{#items}}...{{/items}} 执行条件语句,枚举出里面的对象

      • 按需渲染数据
      • 遍历数据
      • 获取里面的每一项

      如果{{value}}...{{/value}}中的value错误的(下面的),那么将不会被渲染:
      1. 缺少值
      2. false
      3. 0
      4. 字符串
      5. 空集合
      6. 为NSNull

      如果value 是集合的话

      1. datas:
      2. [
      3. "friends": [
      4. [ "name": "Hulk Hogan" ],
      5. [ "name": "Albert Einstein" ],
      6. [ "name": "Tom Selleck" ],
      7. ]
      8. ]
      9. -------------
      10. template:
      11. {{# friends }}
      12. - {{ name }}
      13. {{/ friends }}
      14. -------------
      15. rendering:
      16. - Hulk Hogan
      17. - Albert Einstein
      18. - Tom Selleck

      上面所说的集合可以是Arrays,ranges, sets, NSArray, NSSet等

      其他值既不是falsey, 也不是collection,那么这个块就会被渲染一次:

      1. datas:
      2. [
      3. "user": [
      4. "name": "Mario"
      5. "score": 1500
      6. ]
      7. ]
      8. -------------
      9. template:
      10. {{# user }}
      11. - {{ name }}
      12. - {{ score }}
      13. {{/ user }}
      14. --------------
      15. rendering:
      16. - Mario
      17. - 1500
    2. {{^items}}...{{/items}} 对应于上方的对立面

      1. data:
      2. [
      3. "persons": []
      4. ]
      5. --------------
      6. template:
      7. {{# persons }}
      8. - {{name}} is {{#alive}}alive{{/alive}}{{^alive}}dead{{/alive}}.
      9. {{/ persons }}
      10. {{^ persons }}
      11. Nobody
      12. {{/ persons }}
      13. -------------
      14. rendering:
      15. Nobody
      16. --------------
      17. data:
      18. [
      19. "persons": [
      20. ["name": "Errol Flynn", "alive": false],
      21. ["name": "Sacha Baron Cohen", "alive": true]
      22. ]
      23. ]
      24. ---------------
      25. rendering:
      26. - Errol Flynn is dead.
      27. - Sacha Baron Cohen is alive.
  • Partial Tags

    1. {{>partial}} , 嵌入其他模块

      1. douc.mustanche
      2. Guests:
      3. {{# guests }}
      4. {{> person }}
      5. {{/ guests }}
      6. --------------
      7. person.mustache
      8. {{ name }}
      9. -------------
      10. data:
      11. [
      12. "guests": [
      13. ["name": "Frank Zappa"],
      14. ["name": "Lionel Richie"]
      15. ]
      16. ]
      17. --------------
      18. rendering:
      19. Guests:
      20. - Frank Zappa
      21. - Lionel Richie
  • Partial Override Tags

    1. {{<layout}}...{{/layout}}, 模板继承

      1. layout.mustache // title , 和content 可以被覆盖
      2. <html>
      3. <head>
      4. <title>{{$ title }}Default title{{/ title }}</title>
      5. </head>
      6. <body>
      7. <h1>{{$ title }}Default title{{/ title }}</h1>
      8. {{$ content }}
      9. Default content
      10. {{/ content }}}
      11. </body>
      12. </html>
      13. ---------------
      14. article.mustuche:
      15. {{< layout }}
      16. {{$ title }}{{ article.title }}{{/ title }}
      17. {{$ content }}
      18. {{{ article.html_body }}}
      19. <p>by {{ article.author }}</p>
      20. {{/ content }}
      21. {{/ layout }}
      22. ---------------
      23. data:
      24. [
      25. "article": [
      26. "title": "The 10 most amazing handlebars",
      27. "html_body": "<p>...</p>",
      28. "author": "John Doe"
      29. ]
      30. ]
      31. ----------------
      32. rendering:
      33. <html>
      34. <head>
      35. <title>The 10 most amazing handlebars</title>
      36. </head>
      37. <body>
      38. <h1>The 10 most amazing handlebars</h1>
      39. <p>...</p>
      40. <p>by John Doe</p>
      41. </body>
      42. </html>

      {{$ title }}...{{/ title }}每次都会被渲染,并且只被渲染一次, no boolean checks, no collection iteration.

      一个模板可以包含很多partial override tags

      避免循环

  • Set Delimiters Tags

    1. {{=<% %>=}}, 让你改变标签分隔符。
  • Comment Tags

    1. {{! Wow. Such comment. }} , 注释
  • paragma Tags

    1. {{% CONTENT_TYPE:TEXT }}
    2. {{% CONTENT_TYPE:HTML }}

参考:GRMustache.swift

转载于:https://www.cnblogs.com/Ohero/p/5493881.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/610813
推荐阅读
相关标签
  

闽ICP备14008679号