About template language Mustache

Source: Internet
Author: User

@Fenng A client-rendering Framework for Facebook by Changhao JIANGHTTP://T.CO/NIO6VCD

Fenng recommended a template language: mustache (meaning beard).

Mustache Official website is so introduced:

Logic-less templates.

Available in Ruby, JavaScript, Python,erlang, PHP, Perl, Objective-c, Java,. net,android, C + +, go, Lua, OOC, ActionScript, ColdFusion, Scala, Clojure, Fantom,coffeescript, D, and for Node.js.

Works great with TextMate, Vim, Emacs, Andcoda.

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

To sum up, many languages are supported, and the author is Dr Chiang of Facebook.

See http://mustache.github.com/mustache.5.html for all usage details

GitHub's HTML eggs:

<!--_ _ _____*~~~ * ~~~*_____ __* ___ |/__/| ___ *__ _*/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| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~--> 

Personally feel much better than smarty. Not for anything else, for simplicity and clarity.

It is rumored that watercress (http://shuo.douban.com) used the template. Attention in

Briefly describes the following usage:

1 A typical mustache template:

Hello {{name}} You
have just won ${{value}}!
{{#in_ca}}}
So, ${{taxed_value}}, after taxes.
{{/in_ca}}}

Given the following hash:

{
  "name": "Chris",
  "value": 10000,
  "Taxed_value": 10000-(10000 * 0.4),
  "In_ca": True
}

Would produce the following:

Hello Chris You
have just won $10000!
So, $6000.0, after taxes.
Mustache can be used anywhere, including the HTML configuration file source code. A variable in a template can be rendered by supplying a hash or an object. The template has no if-else,for-loop tag, only tags (tag).
Commonly used tags have labels similar to the syntax of {{name}},{{#person}}. If you do not supply a value, it is not rendered. {{{{html}}} and {{& html}} will render HTML content that is not escaped.
Zone rendering through {{#person}}} ... {{/person}} to implement. For example
Shown.
{{#nothin}}}
  Never shown!
{{/nothin}}}
Output shown. (If no nothin provided)
If a non-empty list or array is provided, the area rendering repeats the render list or each item of the array. For example

Template:

{{#repo}}}
  <b>{{name}}</b>
{/repo}}

Hash:

{'
  repo ': [
    {' name ': ' Resque '},
    {' name ': ' Hub '},
    {' name ': ' RIP '},
  ]
}

Output:

<b>resque</b>
<b>hub</b>
<b>rip</b>
In addition, mustache supports lambda expressions

Template:

{{#wrapped}}}
  {{name}} is awesome.
{{/wrapped}}}

Hash:

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

Output:

<b>willy is awesome.</b>
It is also convenient to make comments: {{! ignore me}}
Import other files as long as this:
Base.mustache:
will be able to output
 
The other {}} can also be configured on its own.
It seems that there is no more to introduce, so much. Like Python, it's simple and straightforward.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.