[原]关于模板语言 Mustache

2011-04-03 10:12

[原]关于模板语言 Mustache

by soasme

at 2011-04-03 02:12:00

original http://blog.csdn.net/soasme/article/details/6297467

@Fenng A client-rendering framework for Facebook by Changhao Jianghttp://t.co/NIo6vCd

 

Fenng推荐了一款模板语言:mustache(意思是胡须)。

 

mustache官网是这么介绍的:

 

Logic-less templates.

Available in RubyJavaScriptPython,ErlangPHPPerlObjective-CJava.NET,AndroidC++GoLuaoocActionScript,ColdFusionScalaClojureFantom,CoffeeScriptD, and for node.js.

Works great with TextMateVimEmacs, andCoda.

The Manual: mustache(5) and mustache(1)

 

总而言之,支持很多语言,作者是facebook的蒋博士。

 

全部用法详见http://mustache.github.com/mustache.5.html

github的html彩蛋:

<!-- _ _ _____*~~~ ** ~~~*_____ __* ___ |/__/| ___ *__ _* / 888~~/__(8OO8)__/~~888 / *_ _* /88888888888888888888888888/ *_ * |8888888888888888888888888888| * /~* /8888/~/88/~/8888/~/88/~/8888/ *~ / ~* /88/ // (88) // /88/ *~ / ~* // // // *~ / ~~*_ _*~~/ / ~~~~~*___ ** ___*~~~~~ / / ~ ~ / / / / / / / / t__n__r__ / / | ####### | / ___ | ####### | ____i__ / / _____p_____l_l____ | ####### | | ooooo | qp i__p__ / | ############## | | ####### |__l___xp____| ooooo | |~~~~| oooo |_I_| ############## | | ####### |oo%Xoox%ooxo| ooooo |p__h__|##%#| oooo |ooo| ############## | | ####### |o%xo%%xoooo%| ooooo | |#xx%| oooo |ooo| ############## | | ####### |o%ooxx%ooo%%| ooooo |######|x##%| oooo |ooo| ############## | | ####### |oo%%x%oo%xoo| ooooo |######|##%x| oooo |ooo| ############## | | ####### |%x%%oo%/oo%o| ooooo |######|/#%x| oooo |ooo| ############## | | ####### |%%x/oo/xx%xo| ooooo |######|#%x/| oooo |ooo| ############## | | ####### |xxooo%%/xo%o| ooooo |######|#^x#| oooo |ooo| ############## | | ####### |oox%%o/x%%ox| ooooo |~~~$~~|x##/| oooo |ooo| ############## | | ####### |x%oo%x/o%//x| ooooo |_KKKK_|#x/%| ooo~/|ooo|~/############## | ~/####### |oox%xo%%oox%~/ooooo |_|~|~/|xx%/| ooo ||oHo| |####AAAA###### |h||##XX### |x%x%WWx%%/ox||ooDoo |_| |Y||xGGx| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~ --> 

个人觉得比smarty好用多了。不为别的,因为简洁明了。

传闻 豆瓣说(http://shuo.douban.com) 运用了这种模板?关注中

简要介绍下用法:

 

1A typical Mustache template:

 

Hello 
You have just won $!

Well, $, after taxes.
 

Given the following hash:

{
  "name": "Chris",
  "value": 10000,
  "taxed_value": 10000 - (10000 * 0.4),
  "in_ca": true
} 

Will produce the following:

Hello Chris
You have just won $10000!
Well, $6000.0, after taxes. 
Mustache可以用在包括html 配置文件 源代码之类的任何地方。通过提供hash或者对象可以渲染出模板中的变量。模板没有if-else,for-loop标记,只有标记(tag)。
常用标签有类似,这样语法的标签.如果不提供值,将不会渲染出来。}和将会渲染出没有转义的html内容。
区域渲染通过 ... 来实现。例如
Shown.

  Never shown!
 
输出Shown.(如果没有提供nothin)
如果提供了非空列表或者数组,区域渲染将会重复渲染列表或数组每一项。例如

Template:


  <b></b>
 

Hash:

{
  "repo": [
    { "name": "resque" },
    { "name": "hub" },
    { "name": "rip" },
  ]
} 

Output:

<b>resque</b>
<b>hub</b>
<b>rip</b> 
另外,Mustache支持lambda表达式

Template:


   is awesome.
 

Hash:

{
  "name": "Willy",
  "wrapped": function() {
    return function(text) {
      return "<b>" + render(text) + "</b>"
    }
  }
} 

Output:

<b>Willy is awesome.</b> 
打注释也很方便:
导入别的文件只要像这样:
base.mustache:
<h2>Names</h2>

  


user.mustache:
<strong></strong> 
便能输出
<h2>Names</h2>

  <strong></strong>
 
另外的另外也是可以自行配置的!
好像没有更多要介绍了,就这么多了。和python一样,简洁明了。

 

        <div>
            作者:soasme 发表于2011-4-2 11:12:00 <a href="http://blog.csdn.net/soasme/article/details/6297467">原文链接</a>
        </div>
        <div>
        阅读:2775 评论:0 <a href="http://blog.csdn.net/soasme/article/details/6297467#comments">查看评论</a>
        </div>