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

14 лет назад
16 лет назад
13 лет назад
13 лет назад
16 лет назад
13 лет назад
16 лет назад
13 лет назад
16 лет назад
14 лет назад
16 лет назад
13 лет назад
14 лет назад
13 лет назад
16 лет назад
14 лет назад
16 лет назад
13 лет назад
16 лет назад
16 лет назад
16 лет назад
14 лет назад
16 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
13 лет назад
16 лет назад
14 лет назад
16 лет назад
13 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
13 лет назад
16 лет назад
13 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
14 лет назад
13 лет назад
14 лет назад
14 лет назад
13 лет назад
14 лет назад
13 лет назад
14 лет назад
13 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
13 лет назад
14 лет назад
13 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
13 лет назад
14 лет назад
14 лет назад
13 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
13 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
13 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
13 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
13 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
14 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
13 лет назад
16 лет назад
14 лет назад
16 лет назад
13 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
16 лет назад
13 лет назад
16 лет назад
13 лет назад
16 лет назад
14 лет назад
16 лет назад
14 лет назад
13 лет назад
14 лет назад
14 лет назад
14 лет назад
13 лет назад
14 лет назад
14 лет назад
14 лет назад
13 лет назад
14 лет назад
14 лет назад
13 лет назад
14 лет назад
13 лет назад
14 лет назад
14 лет назад
14 лет назад
13 лет назад
14 лет назад
13 лет назад
14 лет назад
13 лет назад
14 лет назад
14 лет назад
14 лет назад
14 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 [mustache](http://mustache.github.com/) template system in JavaScript.
  4. [Mustache](http://mustache.github.com/) is a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object.
  5. We call it "logic-less" because there are no if statements, else clauses, or for loops. Instead there are only tags. Some tags are replaced with a value, some nothing, and others a series of values.
  6. For a language-agnostic overview of mustache's template syntax, see the `mustache(5)` [manpage](http://mustache.github.com/mustache.5.html).
  7. ## Where to use mustache.js?
  8. You can use mustache.js to render mustache templates anywhere you can use JavaScript. This includes web browsers, server-side environments such as [node](http://nodejs.org/), and [CouchDB](http://couchdb.apache.org/) views.
  9. mustache.js ships with support for both the [CommonJS](http://www.commonjs.org/) module API and the [Asynchronous Module Definition](https://github.com/amdjs/amdjs-api/wiki/AMD) API, or AMD.
  10. And this will be your templates after you use Mustache:
  11. !['stache](http://d24w6bsrhbeh9d.cloudfront.net/photo/aZPNGon_460sa.gif)
  12. ## Who uses mustache.js?
  13. An updated list of mustache.js users is kept [on the Github wiki](http://wiki.github.com/janl/mustache.js/beard-competition). Add yourself or your company if you use mustache.js!
  14. ## Usage
  15. Below is quick example how to use mustache.js:
  16. var view = {
  17. title: "Joe",
  18. calc: function () {
  19. return 2 + 4;
  20. }
  21. };
  22. var output = Mustache.render("{{title}} spends {{calc}}", view);
  23. In this example, the `Mustache.render` function takes two parameters: 1) the [mustache](http://mustache.github.com/) template and 2) a `view` object that contains the data and code needed to render the template.
  24. ## Templates
  25. A [mustache](http://mustache.github.com/) template is a string that contains any number of mustache tags. Tags are indicated by the double mustaches that surround them. `{{person}}` is a tag, as is `{{#person}}`. In both examples we refer to `person` as the tag's key.
  26. There are several types of tags available in mustache.js.
  27. ### Variables
  28. The most basic tag type is a simple variable. A `{{name}}` tag renders the value of the `name` key in the current context. If there is no such key, nothing is rendered.
  29. All variables are HTML-escaped by default. If you want to render unescaped HTML, use the triple mustache: `{{{name}}}`. You can also use `&` to unescape a variable.
  30. View:
  31. {
  32. "name": "Chris",
  33. "company": "<b>GitHub</b>"
  34. }
  35. Template:
  36. * {{name}}
  37. * {{age}}
  38. * {{company}}
  39. * {{{company}}}
  40. * {{&company}}
  41. Output:
  42. * Chris
  43. *
  44. * &lt;b&gt;GitHub&lt;/b&gt;
  45. * <b>GitHub</b>
  46. * <b>GitHub</b>
  47. JavaScript's dot notation may be used to access keys that are properties of objects in a view.
  48. View:
  49. {
  50. "name": {
  51. "first": "Michael",
  52. "last": "Jackson"
  53. },
  54. "age": "RIP"
  55. }
  56. Template:
  57. * {{name.first}} {{name.last}}
  58. * {{age}}
  59. Output:
  60. * Michael Jackson
  61. * RIP
  62. ### Sections
  63. Sections render blocks of text one or more times, depending on the value of the key in the current context.
  64. A section begins with a pound and ends with a slash. That is, `{{#person}}` begins a `person` section, while `{{/person}}` ends it. The text between the two tags is referred to as that section's "block".
  65. The behavior of the section is determined by the value of the key.
  66. #### False Values or Empty Lists
  67. If the `person` key does not exist, or exists and has a value of `null`, `undefined`, or `false`, or is an empty list, the block will not be rendered.
  68. View:
  69. {
  70. "person": false
  71. }
  72. Template:
  73. Shown.
  74. {{#person}}
  75. Never shown!
  76. {{/person}}
  77. Output:
  78. Shown.
  79. #### Non-Empty Lists
  80. If the `person` key exists and is not `null`, `undefined`, or `false`, and is not an empty list the block will be rendered one or more times.
  81. When the value is a list, the block is rendered once for each item in the list. The context of the block is set to the current item in the list for each iteration. In this way we can loop over collections.
  82. View:
  83. {
  84. "stooges": [
  85. { "name": "Moe" },
  86. { "name": "Larry" },
  87. { "name": "Curly" }
  88. ]
  89. }
  90. Template:
  91. {{#stooges}}
  92. <b>{{name}}</b>
  93. {{/stooges}}
  94. Output:
  95. <b>Moe</b>
  96. <b>Larry</b>
  97. <b>Curly</b>
  98. When looping over an array of strings, a `.` can be used to refer to the current item in the list.
  99. View:
  100. {
  101. "musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"]
  102. }
  103. Template:
  104. {{#musketeers}}
  105. * {{.}}
  106. {{/musketeers}}
  107. Output:
  108. * Athos
  109. * Aramis
  110. * Porthos
  111. * D'Artagnan
  112. If the value of a section variable is a function, it will be called in the context of the current item in the list on each iteration.
  113. View:
  114. {
  115. "beatles": [
  116. { "firstName": "John", "lastName": "Lennon" },
  117. { "firstName": "Paul", "lastName": "McCartney" },
  118. { "firstName": "George", "lastName": "Harrison" },
  119. { "firstName": "Ringo", "lastName": "Starr" }
  120. ],
  121. "name": function () {
  122. return this.firstName + " " + this.lastName;
  123. }
  124. }
  125. Template:
  126. {{#beatles}}
  127. * {{name}}
  128. {{/beatles}}
  129. Output:
  130. * John Lennon
  131. * Paul McCartney
  132. * George Harrison
  133. * Ringo Starr
  134. #### Functions
  135. If the value of a section key is a function, it is called with the section's literal block of text, un-rendered, as its first argument. The second argument is a special rendering function that uses the current view as its view argument. It is called in the context of the current view object.
  136. View:
  137. {
  138. "name": "Tater",
  139. "bold": function () {
  140. return function (text, render) {
  141. return "<b>" + render(text) + "</b>";
  142. }
  143. }
  144. }
  145. Template:
  146. {{#bold}}Hi {{name}}.{{/bold}}
  147. Output:
  148. <b>Hi Tater.</b>
  149. ### Inverted Sections
  150. An inverted section opens with `{{^section}}` instead of `{{#section}}`. The block of an inverted section is rendered only if the value of that section's tag is `null`, `undefined`, `false`, or an empty list.
  151. View:
  152. {
  153. "repos": []
  154. }
  155. Template:
  156. {{#repos}}<b>{{name}}</b>{{/repos}}
  157. {{^repos}}No repos :({{/repos}}
  158. Output:
  159. No repos :(
  160. ### Comments
  161. Comments begin with a bang and are ignored. The following template:
  162. <h1>Today{{! ignore me }}.</h1>
  163. Will render as follows:
  164. <h1>Today.</h1>
  165. Comments may contain newlines.
  166. ### Partials
  167. Partials begin with a greater than sign, like {{> box}}.
  168. Partials are rendered at runtime (as opposed to compile time), so recursive partials are possible. Just avoid infinite loops.
  169. They also inherit the calling context. Whereas in ERB you may have this:
  170. <%= partial :next_more, :start => start, :size => size %>
  171. Mustache requires only this:
  172. {{> next_more}}
  173. Why? Because the `next_more.mustache` file will inherit the `size` and `start` variables from the calling context. In this way you may want to think of partials as includes, or template expansion, even though it's not literally true.
  174. For example, this template and partial:
  175. base.mustache:
  176. <h2>Names</h2>
  177. {{#names}}
  178. {{> user}}
  179. {{/names}}
  180. user.mustache:
  181. <strong>{{name}}</strong>
  182. Can be thought of as a single, expanded template:
  183. <h2>Names</h2>
  184. {{#names}}
  185. <strong>{{name}}</strong>
  186. {{/names}}
  187. In mustache.js an object of partials may be passed as the third argument to `Mustache.render`. The object should be keyed by the name of the partial, and its value should be the partial text.
  188. ### Set Delimiter
  189. Set Delimiter tags start with an equals sign and change the tag delimiters from `{{` and `}}` to custom strings.
  190. Consider the following contrived example:
  191. * {{ default_tags }}
  192. {{=<% %>=}}
  193. * <% erb_style_tags %>
  194. <%={{ }}=%>
  195. * {{ default_tags_again }}
  196. Here we have a list with three items. The first item uses the default tag style, the second uses ERB style as defined by the Set Delimiter tag, and the third returns to the default style after yet another Set Delimiter declaration.
  197. According to [ctemplates](http://google-ctemplate.googlecode.com/svn/trunk/doc/howto.html), this "is useful for languages like TeX, where double-braces may occur in the text and are awkward to use for markup."
  198. Custom delimiters may not contain whitespace or the equals sign.
  199. ### Compiled Templates
  200. Mustache templates can be compiled into JavaScript functions using `Mustache.compile` for improved rendering performance.
  201. If you have template views that are rendered multiple times, compiling your template into a JavaScript function will minimise the amount of work required for each re-render.
  202. Pre-compiled templates can also be generated server-side, for delivery to the browser as ready to use JavaScript functions, further reducing the amount of client side processing required for initialising templates.
  203. **Mustache.compile**
  204. Use `Mustache.compile` to compile standard Mustache string templates into reusable Mustache template functions.
  205. var compiledTemplate = Mustache.compile(stringTemplate);
  206. The function returned from `Mustache.compile` can then be called directly, passing in the template data as an argument (with an object of partials as an optional second parameter), to generate the final output.
  207. var templateOutput = compiledTemplate(templateData);
  208. **Mustache.compilePartial**
  209. Template partials can also be compiled using the `Mustache.compilePartial` function. The first parameter of this function, is the name of the partial as it appears within parent templates.
  210. Mustache.compilePartial('partial-name', stringTemplate);
  211. Compiled partials are then available to both `Mustache.render` and `Mustache.compile`.
  212. ## Plugins for JavaScript Libraries
  213. mustache.js may be built specifically for several different client libraries, including the following:
  214. - [jQuery](http://jquery.com/)
  215. - [MooTools](http://mootools.net/)
  216. - [Dojo](http://www.dojotoolkit.org/)
  217. - [YUI](http://developer.yahoo.com/yui/)
  218. - [qooxdoo](http://qooxdoo.org/)
  219. These may be built using [Rake](http://rake.rubyforge.org/) and one of the following commands:
  220. $ rake jquery
  221. $ rake mootools
  222. $ rake dojo
  223. $ rake yui3
  224. $ rake qooxdoo
  225. ## Testing
  226. The mustache.js test suite uses the [mocha](http://visionmedia.github.com/mocha/) testing framework. In order to run the tests you'll need to install [node](http://nodejs.org/). Once that's done you can install mocha using [npm](http://npmjs.org/).
  227. $ npm install -g mocha
  228. You also need to install the sub module containing [Mustache specifications](http://github.com/mustache/spec) in the project root.
  229. $ git submodule init
  230. $ git submodule update
  231. Then run the tests.
  232. $ mocha test
  233. The test suite consists of both unit and integration tests. If a template isn't rendering correctly for you, you can make a test for it by doing the following:
  234. 1. Create a template file named `mytest.mustache` in the `test/_files`
  235. directory. Replace `mytest` with the name of your test.
  236. 2. Create a corresponding view file named `mytest.js` in the same directory.
  237. This file should contain a JavaScript object literal enclosed in
  238. parentheses. See any of the other view files for an example.
  239. 3. Create a file with the expected output in `mytest.txt` in the same
  240. directory.
  241. Then, you can run the test with:
  242. $ TEST=mytest mocha test/render_test.js
  243. ## Thanks
  244. mustache.js wouldn't kick ass if it weren't for these fine souls:
  245. * Chris Wanstrath / defunkt
  246. * Alexander Lang / langalex
  247. * Sebastian Cohnen / tisba
  248. * J Chris Anderson / jchris
  249. * Tom Robinson / tlrobinson
  250. * Aaron Quint / quirkey
  251. * Douglas Crockford
  252. * Nikita Vasilyev / NV
  253. * Elise Wood / glytch
  254. * Damien Mathieu / dmathieu
  255. * Jakub Kuźma / qoobaa
  256. * Will Leinweber / will
  257. * dpree
  258. * Jason Smith / jhs
  259. * Aaron Gibralter / agibralter
  260. * Ross Boucher / boucher
  261. * Matt Sanford / mzsanford
  262. * Ben Cherry / bcherry
  263. * Michael Jackson / mjijackson