mustache模板引擎

Logic-less templates.

githubhttp://mustache.github.com/ 
文档:http://mustache.github.com/mustache.5.html 

http://mustache.github.io

Java 版 Mustache SVN 源码 
http://jmustache.googlecode.com/svn   

mustache 类似  freemark和valicity 模板引擎,不过mustache 更轻量级,支持语言: RubyJavaScriptPython,Erlangnode.jsPHPPerlPerl6Objective-CJavaC#/.NETAndroidC++GoLua,oocActionScriptColdFusionScalaClojure,FantomCoffeeScriptDHaskellXQuery,ASPIoDartHaxeDelphiRacketRust,OCamlSwift, and for Bash

 mustache 支持功能比较弱,不过我们可以建立在mustache 之上进行扩展实现。

mustache的特点就是很语法很简单,主要语法如下

  • {{ name }} 打印变量,默认是escape过的,如果不要escape,用3个分隔符 {{{ name }}},或者用 {{ &name }},这个和分隔符无关
  • {{#person}}…{{/person}} 区块,4种方式
    • person是真假值,决定是否输出
    • person 是list of array,会循环展开 for x in person:section.render('xxx)
    • person 是匿名函数/object, 区块包裹的html 会作为参数传递进去
    • person 是dict,直接打印 dict[key]
  • {{^person}}…{{/person},反向区块
  • {{!name }} 注释
  • {{> box }} 载入子模块

Template:

{{#person?}}
  Hi {{name}}!
{{/person?}}

Hash:

{
  "person?": { "name": "Jon" }
}

Output:

Hi Jon!

Template:

{{#repo}}
  <b>{{name}}</b>  JS-USE:{{#first}}++{{/first}}  JAVA-USE: first:{{-first}} last:{{-last}} index:{{-index}}
 {{/repo}}
{{^repo}}
  No repos :(
{{/repo}}

Hash:

{
  "repo": []
}

Output:

No repos :(


猜你喜欢

转载自blog.csdn.net/kevin_Luan/article/details/46485561