25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 10KB

14 년 전
16 년 전
14 년 전
16 년 전
14 년 전
14 년 전
16 년 전
14 년 전
16 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
14 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. ## Who uses mustache.js?
  11. 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!
  12. ## Usage
  13. Below is quick example how to use mustache.js:
  14. var view = {
  15. title: "Joe",
  16. calc: function () {
  17. return 2 + 4;
  18. }
  19. };
  20. var output = Mustache.render("{{title}} spends {{calc}}", view);
  21. 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.
  22. ## Templates
  23. 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.
  24. There are several types of tags available in mustache.js.
  25. ### Variables
  26. 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.
  27. 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.
  28. View:
  29. {
  30. "name": "Chris",
  31. "company": "<b>GitHub</b>"
  32. }
  33. Template:
  34. * {{name}}
  35. * {{age}}
  36. * {{company}}
  37. * {{{company}}}
  38. * {{&company}}
  39. Output:
  40. * Chris
  41. *
  42. * &lt;b&gt;GitHub&lt;/b&gt;
  43. * <b>GitHub</b>
  44. * <b>GitHub</b>
  45. JavaScript's dot notation may be used to access keys that are properties of objects in a view.
  46. View:
  47. {
  48. "name": {
  49. "first": "Michael",
  50. "last": "Jackson"
  51. },
  52. "age": "RIP"
  53. }
  54. Template:
  55. * {{name.first}} {{name.last}}
  56. * {{age}}
  57. Output:
  58. * Michael Jackson
  59. * RIP
  60. ### Sections
  61. Sections render blocks of text one or more times, depending on the value of the key in the current context.
  62. 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".
  63. The behavior of the section is determined by the value of the key.
  64. #### False Values or Empty Lists
  65. If the `person` key exists and has a value of `null`, `undefined`, or `false`, or is an empty list, the block will not be rendered.
  66. View:
  67. {
  68. "person": false
  69. }
  70. Template:
  71. Shown.
  72. {{#person}}
  73. Never shown!
  74. {{/person}}
  75. Output:
  76. Shown.
  77. #### Non-Empty Lists
  78. 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.
  79. 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.
  80. View:
  81. {
  82. "stooges": [
  83. { "name": "Moe" },
  84. { "name": "Larry" },
  85. { "name": "Curly" }
  86. ]
  87. }
  88. Template:
  89. {{#stooges}}
  90. <b>{{name}}</b>
  91. {{/stooges}}
  92. Output:
  93. <b>Moe</b>
  94. <b>Larry</b>
  95. <b>Curly</b>
  96. When looping over an array of strings, a `.` can be used to refer to the current item in the list.
  97. View:
  98. {
  99. "musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"]
  100. }
  101. Template:
  102. {{#musketeers}}
  103. * {{.}}
  104. {{/musketeers}}
  105. Output:
  106. * Athos
  107. * Aramis
  108. * Porthos
  109. * D'Artagnan
  110. 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.
  111. View:
  112. {
  113. "beatles": [
  114. { "firstName": "John", "lastName": "Lennon" },
  115. { "firstName": "Paul", "lastName": "McCartney" },
  116. { "firstName": "George", "lastName": "Harrison" },
  117. { "firstName": "Ringo", "lastName": "Starr" }
  118. ],
  119. "name": function () {
  120. return this.firstName + " " + this.lastName;
  121. }
  122. }
  123. Template:
  124. {{#beatles}}
  125. * {{name}}
  126. {{/beatles}}
  127. Output:
  128. * John Lennon
  129. * Paul McCartney
  130. * George Harrison
  131. * Ringo Starr
  132. #### Functions
  133. 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.
  134. View:
  135. {
  136. "name": "Tater",
  137. "bold": function () {
  138. return function (text, render) {
  139. return "<b>" + render(text) + "</b>";
  140. }
  141. }
  142. }
  143. Template:
  144. {{#bold}}Hi {{name}}.{{/bold}}
  145. Output:
  146. <b>Hi Tater.</b>
  147. ### Inverted Sections
  148. 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.
  149. View:
  150. {
  151. "repos": []
  152. }
  153. Template:
  154. {{#repos}}<b>{{name}}</b>{{/repos}}
  155. {{^repos}}No repos :({{/repos}}
  156. Output:
  157. No repos :(
  158. ### Comments
  159. Comments begin with a bang and are ignored. The following template:
  160. <h1>Today{{! ignore me }}.</h1>
  161. Will render as follows:
  162. <h1>Today.</h1>
  163. Comments may contain newlines.
  164. ### Partials
  165. Partials begin with a greater than sign, like {{> box}}.
  166. Partials are rendered at runtime (as opposed to compile time), so recursive partials are possible. Just avoid infinite loops.
  167. They also inherit the calling context. Whereas in ERB you may have this:
  168. <%= partial :next_more, :start => start, :size => size %>
  169. Mustache requires only this:
  170. {{> next_more}}
  171. 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.
  172. For example, this template and partial:
  173. base.mustache:
  174. <h2>Names</h2>
  175. {{#names}}
  176. {{> user}}
  177. {{/names}}
  178. user.mustache:
  179. <strong>{{name}}</strong>
  180. Can be thought of as a single, expanded template:
  181. <h2>Names</h2>
  182. {{#names}}
  183. <strong>{{name}}</strong>
  184. {{/names}}
  185. 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.
  186. ### Set Delimiter
  187. Set Delimiter tags start with an equals sign and change the tag delimiters from `{{` and `}}` to custom strings.
  188. Consider the following contrived example:
  189. * {{ default_tags }}
  190. {{=<% %>=}}
  191. * <% erb_style_tags %>
  192. <%={{ }}=%>
  193. * {{ default_tags_again }}
  194. 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.
  195. 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."
  196. Custom delimiters may not contain whitespace or the equals sign.
  197. ## Plugins for JavaScript Libraries
  198. mustache.js may be built specifically for several different client libraries, including the following:
  199. - [jQuery](http://jquery.com/)
  200. - [MooTools](http://mootools.net/)
  201. - [Dojo](http://www.dojotoolkit.org/)
  202. - [YUI](http://developer.yahoo.com/yui/)
  203. - [qooxdoo](http://qooxdoo.org/)
  204. These may be built using [Rake](http://rake.rubyforge.org/) and one of the following commands:
  205. $ rake jquery
  206. $ rake mootools
  207. $ rake dojo
  208. $ rake yui
  209. $ rake qooxdoo
  210. ## Testing
  211. The mustache.js test suite uses the [vows](http://vowsjs.org/) testing framework. In order to run the tests you'll need to install [node](http://nodejs.org/). Once that's done you can install vows using [npm](http://npmjs.org/).
  212. $ npm install -g vows
  213. Then run the tests.
  214. $ vows --spec
  215. 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:
  216. 1. Create a template file named `mytest.mustache` in the `test/_files`
  217. directory. Replace `mytest` with the name of your test.
  218. 2. Create a corresponding view file named `mytest.js` in the same directory.
  219. This file should contain a JavaScript object literal enclosed in
  220. parentheses. See any of the other view files for an example.
  221. 3. Create a file with the expected output in `mytest.txt` in the same
  222. directory.
  223. Then, you can run the test with:
  224. $ TEST=mytest vows test/render_test.js
  225. ## Thanks
  226. mustache.js wouldn't kick ass if it weren't for these fine souls:
  227. * Chris Wanstrath / defunkt
  228. * Alexander Lang / langalex
  229. * Sebastian Cohnen / tisba
  230. * J Chris Anderson / jchris
  231. * Tom Robinson / tlrobinson
  232. * Aaron Quint / quirkey
  233. * Douglas Crockford
  234. * Nikita Vasilyev / NV
  235. * Elise Wood / glytch
  236. * Damien Mathieu / dmathieu
  237. * Jakub Kuźma / qoobaa
  238. * Will Leinweber / will
  239. * dpree
  240. * Jason Smith / jhs
  241. * Aaron Gibralter / agibralter
  242. * Ross Boucher / boucher
  243. * Matt Sanford / mzsanford
  244. * Ben Cherry / bcherry
  245. * Michael Jackson / mjijackson