About the template language mustache

Source: Internet
Author: User

@ Fenng a client-rendering framework for Facebook by changhao jianghttp: // T. CO/nio6vcd

 

Fenng recommends a template language: mustache (beard ).

 

The mustache official website introduces the following:

 

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)

 

All in all, there are many languages supported by Dr. Jiang from Facebook.

 

For more information, see http://mustache.github.com/mustache.5.html.

GitHub HTML eggs:

<! -- <Br/>__ <br/> _____*~~~ **~~~ * _____ <Br/> _ * ___ |/_/| ___ * __< br/> _ */888 ~~ /_ (8oo8 )__/~~ 888/* _ <br/> _ */88888888888888888888888888/* _ <br/> * | 8888888888888888888888888888 | * <br/> /~ */8888 /~ /88 /~ /8888 /~ /88 /~ /8888 /*~ <Br/> /~ */88 // (88) // 88 /*~ <Br/> /~ *//////*~ <Br/> /~~ *__*~~ /<Br/> /~~~~~ *___**___*~~~~~ /<Br/> /~ ~ /<Br/> // <br/>/T _ n _ r _/<br/>/| #######| <br/>/___ | ########|____ I _/<br/>/_____ P _____ l_l ____ | # ######| ooooo | QP <br/> I _ p _/| ###############| # #######|__ l ___ XP ____ | ooooo || ~~~~ | <Br/> oooo | _ I _ | ##############|######### | OO % xoox % ooxo | ooooo | P _ H __|###%# | <br/> oooo | OOO |##############| ## #####| o % XO % xoooo % | ooooo | # XX % | <br/> oooo | OOO | ########### ####|####### | o % ooxx % OOO % | ooooo |#######| x ##% | <br/> oooo | OOO | ##############|######## | OO % x % OO % Xoo | ooooo | ## ####### % x | <br/> oooo | OOO | ################| ###### # | % x % OO %/OO % o | ooooo |######|/# % x | <br/> ooo O | OOO | ##############|########| % x/OO/XX % XO | ooooo | ######### % x/| <br/> oooo | OOO | #### ###| xxooo %/XO % o | ooooo | ######### ^ X # | <br/> oooo | OOO | ###### ################# | oox % O/X % ox | ooooo | ~~~ $ ~~ | X ###/ | <br/> oooo | OOO |###############|########| X % OO % x/o % // X | ooooo | _ KKKK _ | # x/% | <br/> OOO ~ /| OOO | ~ /###############| ~ /######## | Oox % XO % oox % ~ /Ooooo | _ | ~ | ~ /| XX %/| <br/> OOO | Oho | |#### AAAA ####### | H ||## XX ###| X % X % wwx %/Ox | oodoo | _ | Y | xggx | <br/> ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~ | ~~~~~~~ <Br/> --> 

I personally think it is much better than smarty. Nothing else, because it is concise and clear.

It is rumored that Douban (http://shuo.douban.com) uses this template? Paying attention

Briefly introduces the usage:

 

1A typical mustache template:

 

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

Given the following hash:

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

Will produce the following:

Hello ChrisYou have just won $10000!Well, $6000.0, after taxes.
Mustache can be used in any place including the source code of the HTML configuration file. You can render the variables in the template by providing the hash or object. The template does not have the IF-Else, for-loop tag, and only the tag ).
Common tags include tags similar to syntax {name} and {# person}. If no value is provided, it will not be rendered. {HTML }}and {& HTML} will render HTML content without escaping.
Region rendering is implemented through {# person}... {/person. For example
Shown.{{#nothin}}  Never shown!{{/nothin}}
Output shown. (If nothin is not provided)
If a non-empty list or array is provided, area rendering repeats rendering of the list or 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 annotate :{{! Ignore me }}
Import other files as long:
base.mustache:<p>Names</p>{{#names}}  {{> user}}{{/names}}user.mustache:<strong>{{name}}</strong>
Output
<p>Names</p>{{#names}}  <strong>{{name}}</strong>{{/names}}
In addition, {} can be configured by yourself!
It seems that there are no more introductions. Like python, It is concise and clear.


 

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.