Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

14 лет назад
16 лет назад
14 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
16 лет назад
16 лет назад
14 лет назад
16 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
15 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
15 лет назад
15 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
16 лет назад
14 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # mustache.js — Logic-less {{mustache}} templates with JavaScript
  2. > What could be more logical awesome than no logic at all?
  3. [mustache.js](http://github.com/janl/mustache.js) is an implementation of the
  4. [Mustache](http://mustache.github.com/) templating system in JavaScript.
  5. [Mustache](http://mustache.github.com/) is a logic-less template syntax. It can
  6. be used for HTML, config files, source code - anything. It works by expanding
  7. tags in a template using values provided in a hash or object.
  8. We call it "logic-less" because there are no if statements, else clauses, or for
  9. loops. Instead there are only tags. Some tags are replaced with a value, some
  10. nothing, and others a series of values.
  11. For a language-agnostic overview of Mustache's template syntax, see the
  12. `mustache(5)` [manpage](http://mustache.github.com/mustache.5.html).
  13. ## Where to use mustache.js?
  14. You can use mustache.js to render templates in many various scenarios where you
  15. can use JavaScript. For example, you can render templates in a browser,
  16. server-side using [node](http://nodejs.org/), in [CouchDB](http://couchdb.apache.org/)
  17. views, or in almost any other environment where you can use JavaScript.
  18. ## Who uses mustache.js?
  19. An updated list of mustache.js users is kept [on the Github wiki](http://wiki.github.com/janl/mustache.js/beard-competition).
  20. Add yourself or your company if you use mustache.js!
  21. ## Usage
  22. Below is quick example how to use mustache.js:
  23. var view = {
  24. title: "Joe",
  25. calc: function() {
  26. return 2 + 4;
  27. }
  28. };
  29. var html = Mustache.to_html("{{title}} spends {{calc}}", view);
  30. In this example, the `Mustache.to_html` function takes two parameters: 1) the
  31. [mustache](http://mustache.github.com/) template and 2) a `view` object that
  32. contains the data and code needed to render the template.
  33. ## Template Tag Types
  34. There are several types of tags currently implemented in mustache.js.
  35. ### Simple Tags
  36. Tags are always surrounded by mustaches like this `{{foobar}}`.
  37. var view = {name: "Joe", say_hello: function(){ return "hello" }}
  38. template = "{{say_hello}}, {{name}}"
  39. #### Accessing values in nested objects (Dot Notation)
  40. To access data logically grouped into nested objects, specify a '.' delimited
  41. path to the value.
  42. var contact = {
  43. name: {first: "Bill", last: "Bobitybob" },
  44. age: 37
  45. }
  46. template = "Hello, {{name.first}} {{name.last}}. You are {{age}} years old."
  47. *NOTICE*: The dot notation feature was recently implemented for the 0.4
  48. release, which is not out as of Nov 9 2011. You can find the feature in the
  49. current master branch of mustachejs.
  50. ### Conditional Sections
  51. Conditional sections begin with `{{#condition}}` and end with
  52. `{{/condition}}`. When `condition` evaluates to true, the section is rendered,
  53. otherwise the whole block will output nothing at all. `condition` may be a
  54. function returning true/false or a simple boolean.
  55. var view = {condition: function() {
  56. // [...your code goes here...]
  57. return true;
  58. }}
  59. {{#condition}}
  60. I will be visible if condition is true
  61. {{/condition}}
  62. ### Enumerable Sections
  63. Enumerable Sections use the same syntax as condition sections do.
  64. `{{#shopping_items}}` and `{{/shopping_items}}`. Actually the view decides how
  65. mustache.js renders the section. If the view returns an array, it will
  66. iterator over the items. Use `{{.}}` to access the current item inside the
  67. enumeration section.
  68. var view = {name: "Joe's shopping card",
  69. items: ["bananas", "apples"]}
  70. var template = "{{name}}: <ul> {{#items}}<li>{{.}}</li>{{/items}} </ul>"
  71. Outputs:
  72. Joe's shopping card: <ul><li>bananas</li><li>apples</li></ul>
  73. ### Higher Order Sections
  74. If a section key returns a function, it will be called and passed both the
  75. unrendered block of text and a renderer convenience function.
  76. Given this object:
  77. "name": "Tater",
  78. "bolder": function() {
  79. return function(text, render) {
  80. return "<b>" + render(text) + '</b>'
  81. }
  82. }
  83. And this template:
  84. {{#bolder}}Hi {{name}}.{{/bolder}}
  85. We'll get this output:
  86. <b>Hi Tater.</b>
  87. As you can see, we’re pre-processing the text in the block. This can be used
  88. to implement caching, filters (like syntax highlighting), etc.
  89. You can use `this.name` to access the attribute `name` from your view.
  90. ### Dereferencing Sections
  91. If your data has components that are logically grouped into nested objects,
  92. you may wish to dereference an object to access its values.
  93. Given this object:
  94. {
  95. "name": "Bill",
  96. "address": {
  97. "street": "801 Streetly street",
  98. "city": "Boston",
  99. "state": "MA",
  100. "zip" "02101"
  101. }
  102. }
  103. And this template:
  104. <h1>Contact: {{name}}</h1>
  105. {{#address}}
  106. <p>{{street}}</p>
  107. <p>{{city}}, {{state}} {{zip}}</p>
  108. {{/address}}
  109. We'll get this output:
  110. <h1>Contact: Bill</h1>
  111. <p>801 Streetly street</p>
  112. <p>Boston, MA 02101</p>
  113. ### Inverted Sections
  114. An inverted section opens with `{{^section}}` instead of `{{#section}}` and
  115. uses a boolean negative to evaluate. Empty arrays are considered falsy.
  116. View:
  117. var inverted_section = {
  118. "repo": []
  119. }
  120. Template:
  121. {{#repo}}<b>{{name}}</b>{{/repo}}
  122. {{^repo}}No repos :({{/repo}}
  123. Result:
  124. No repos :(
  125. ### View Partials
  126. mustache.js supports a quite powerful but yet simple view partial mechanism.
  127. Use the following syntax for partials: `{{>partial_name}}`
  128. var view = {
  129. name: "Joe",
  130. winnings: {
  131. value: 1000,
  132. taxed_value: function() {
  133. return this.value - (this.value * 0.4);
  134. }
  135. }
  136. };
  137. var template = "Welcome, {{name}}! {{>winnings}}"
  138. var partials = {
  139. winnings: "You just won ${{value}} (which is ${{taxed_value}} after tax)"};
  140. var output = Mustache.to_html(template, view, partials)
  141. output will be:
  142. Welcome, Joe! You just won $1000 (which is $600 after tax)
  143. You invoke a partial with `{{>winnings}}`. Invoking the partial `winnings`
  144. will tell mustache.js to look for a object in the context's property
  145. `winnings`. It will then use that object as the context for the template found
  146. in `partials` for `winnings`.
  147. ## Escaping
  148. mustache.js does escape all values when using the standard double mustache
  149. syntax. Characters which will be escaped: `& \ " ' < >`. To disable escaping,
  150. simply use triple mustaches like `{{{unescaped_variable}}}`.
  151. Example: Using `{{variable}}` inside a template for `5 > 2` will result in `5 &gt; 2`, where as the usage of `{{{variable}}}` will result in `5 > 2`.
  152. ## Streaming
  153. To stream template results out of mustache.js, you can pass an optional
  154. `send()` callback to the `to_html()` call:
  155. Mustache.to_html(template, view, partials, function(line) {
  156. print(line);
  157. });
  158. ## Pragmas
  159. Pragma tags let you alter the behaviour of mustache.js. They have the format
  160. of
  161. {{%PRAGMANAME}}
  162. and they accept options:
  163. {{%PRAGMANAME option=value}}
  164. ### IMPLICIT-ITERATOR
  165. When using a block to iterate over an enumerable (Array), mustache.js expects
  166. an objects as enumerable items. The implicit iterator pragma enables optional
  167. behaviour of allowing literals as enumerable items. Consider this view:
  168. var view = {
  169. foo: [1, 2, 3, 4, 5, "french"]
  170. };
  171. The following template can iterate over the member `foo`:
  172. {{%IMPLICIT-ITERATOR}}
  173. {{#foo}}
  174. {{.}}
  175. {{/foo}}
  176. If you don't like the dot in there, the pragma accepts an option to set your
  177. own iteration marker:
  178. {{%IMPLICIT-ITERATOR iterator=bob}}
  179. {{#foo}}
  180. {{bob}}
  181. {{/foo}}
  182. ## Plugins for JavaScript Libraries
  183. mustache.js may be built specifically for several different client libraries
  184. and platforms, including the following:
  185. - [node](http://nodejs.org/) (or other CommonJS platforms)
  186. - [jQuery](http://jquery.com/)
  187. - [Dojo](http://www.dojotoolkit.org/)
  188. - [YUI](http://developer.yahoo.com/yui/)
  189. - [RequireJS](http://requirejs.org/)
  190. - [qooxdoo](http://qooxdoo.org/)
  191. These may be built using [Rake](http://rake.rubyforge.org/) and one of the
  192. following commands:
  193. $ rake commonjs
  194. $ rake jquery
  195. $ rake dojo
  196. $ rake yui
  197. $ rake requirejs
  198. $ rake qooxdoo
  199. ## Thanks
  200. Mustache.js wouldn't kick ass if it weren't for these fine souls:
  201. * Chris Wanstrath / defunkt
  202. * Alexander Lang / langalex
  203. * Sebastian Cohnen / tisba
  204. * J Chris Anderson / jchris
  205. * Tom Robinson / tlrobinson
  206. * Aaron Quint / quirkey
  207. * Douglas Crockford
  208. * Nikita Vasilyev / NV
  209. * Elise Wood / glytch
  210. * Damien Mathieu / dmathieu
  211. * Jakub Kuźma / qoobaa
  212. * Will Leinweber / will
  213. * dpree
  214. * Jason Smith / jhs
  215. * Aaron Gibralter / agibralter
  216. * Ross Boucher / boucher
  217. * Matt Sanford / mzsanford
  218. * Ben Cherry / bcherry
  219. * Michael Jackson / mjijackson