Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

README.md 13KB

14 lat temu
16 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
14 lat temu
16 lat temu
13 lat temu
14 lat temu
13 lat temu
14 lat temu
13 lat temu
11 lat temu
11 lat temu
16 lat temu
14 lat temu
16 lat temu
13 lat temu
14 lat temu
14 lat temu
13 lat temu
13 lat temu
14 lat temu
14 lat temu
14 lat temu
13 lat temu
14 lat temu
14 lat temu
14 lat temu
14 lat temu
13 lat temu
14 lat temu
13 lat temu
14 lat temu
14 lat temu
14 lat temu
14 lat temu
14 lat temu
13 lat temu
14 lat temu
13 lat temu
14 lat temu
14 lat temu
14 lat temu
14 lat temu
13 lat temu
14 lat temu
14 lat temu
14 lat temu
14 lat temu
13 lat temu
14 lat temu
14 lat temu
14 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
14 lat temu
13 lat temu
14 lat temu
14 lat temu
13 lat temu
14 lat temu
14 lat temu
14 lat temu
14 lat temu
13 lat temu
12 lat temu
14 lat temu
14 lat temu
14 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. ## Contributing
  13. mustache.js is a mature project, but it continues to actively invite maintainers. You can help out a high-profile project that is used in a lot of places on the web. There is [plenty](https://github.com/janl/mustache.js/issues) of [work](https://github.com/janl/mustache.js/pulls) to do. No big commitment required, if all you do is review a single [Pull Request](https://github.com/janl/mustache.js/pulls), you are a maintainer. And a hero.
  14. ### Your First Contribution
  15. - review a [Pull Request](https://github.com/janl/mustache.js/pulls)
  16. - fix an [Issue](https://github.com/janl/mustache.js/issues)
  17. - update the [documentation](https://github.com/janl/mustache.js#usage)
  18. - make a website
  19. - write a tutorial
  20. * * *
  21. ## Usage
  22. Below is quick example how to use mustache.js:
  23. ```js
  24. var view = {
  25. title: "Joe",
  26. calc: function () {
  27. return 2 + 4;
  28. }
  29. };
  30. var output = Mustache.render("{{title}} spends {{calc}}", view);
  31. ```
  32. 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.
  33. ## Templates
  34. 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. There are several types of tags available in mustache.js, described below.
  35. There are several techniques that can be used to load templates and hand them to mustache.js, here are two of them:
  36. #### Include Templates
  37. If you need a template for a dynamic part in a static website, you can consider including the template in the static HTML file to avoid loading templates separately. Here's a small example using `jQuery`:
  38. ```html
  39. <html>
  40. <body onload="loadUser">
  41. <div id="target">Loading...</div>
  42. <script id="template" type="x-tmpl-mustache">
  43. Hello {{ name }}!
  44. </script>
  45. </body>
  46. </html>
  47. ```
  48. ```js
  49. function loadUser() {
  50. var template = $('#template').html();
  51. Mustache.parse(template); // optional, speeds up future uses
  52. var rendered = Mustache.render(template, {name: "Luke"});
  53. $('#target').html(rendered);
  54. }
  55. ```
  56. #### Load External Templates
  57. If your templates reside in individual files, you can load them asynchronously and render them when they arrive. Another example using `jQuery`:
  58. ```js
  59. function loadUser() {
  60. $.get('template.mst', function(template) {
  61. var rendered = Mustache.render(template, {name: "Luke"});
  62. $('#target').html(rendered);
  63. });
  64. }
  65. ```
  66. ### Variables
  67. 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.
  68. 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.
  69. View:
  70. ```json
  71. {
  72. "name": "Chris",
  73. "company": "<b>GitHub</b>"
  74. }
  75. ```
  76. Template:
  77. ```html
  78. * {{name}}
  79. * {{age}}
  80. * {{company}}
  81. * {{{company}}}
  82. * {{&company}}
  83. ```
  84. Output:
  85. ```html
  86. * Chris
  87. *
  88. * &lt;b&gt;GitHub&lt;/b&gt;
  89. * <b>GitHub</b>
  90. * <b>GitHub</b>
  91. ```
  92. JavaScript's dot notation may be used to access keys that are properties of objects in a view.
  93. View:
  94. ```json
  95. {
  96. "name": {
  97. "first": "Michael",
  98. "last": "Jackson"
  99. },
  100. "age": "RIP"
  101. }
  102. ```
  103. Template:
  104. ```html
  105. * {{name.first}} {{name.last}}
  106. * {{age}}
  107. ```
  108. Output:
  109. ```html
  110. * Michael Jackson
  111. * RIP
  112. ```
  113. ### Sections
  114. Sections render blocks of text one or more times, depending on the value of the key in the current context.
  115. 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".
  116. The behavior of the section is determined by the value of the key.
  117. #### False Values or Empty Lists
  118. If the `person` key does not exist, or exists and has a value of `null`, `undefined`, `false`, `0`, or `NaN`, or is an empty string or an empty list, the block will not be rendered.
  119. View:
  120. ```json
  121. {
  122. "person": false
  123. }
  124. ```
  125. Template:
  126. ```html
  127. Shown.
  128. {{#person}}
  129. Never shown!
  130. {{/person}}
  131. ```
  132. Output:
  133. ```html
  134. Shown.
  135. ```
  136. #### Non-Empty Lists
  137. 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.
  138. 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.
  139. View:
  140. ```json
  141. {
  142. "stooges": [
  143. { "name": "Moe" },
  144. { "name": "Larry" },
  145. { "name": "Curly" }
  146. ]
  147. }
  148. ```
  149. Template:
  150. ```html
  151. {{#stooges}}
  152. <b>{{name}}</b>
  153. {{/stooges}}
  154. ```
  155. Output:
  156. ```html
  157. <b>Moe</b>
  158. <b>Larry</b>
  159. <b>Curly</b>
  160. ```
  161. When looping over an array of strings, a `.` can be used to refer to the current item in the list.
  162. View:
  163. ```json
  164. {
  165. "musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"]
  166. }
  167. ```
  168. Template:
  169. ```html
  170. {{#musketeers}}
  171. * {{.}}
  172. {{/musketeers}}
  173. ```
  174. Output:
  175. ```html
  176. * Athos
  177. * Aramis
  178. * Porthos
  179. * D'Artagnan
  180. ```
  181. 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.
  182. View:
  183. ```js
  184. {
  185. "beatles": [
  186. { "firstName": "John", "lastName": "Lennon" },
  187. { "firstName": "Paul", "lastName": "McCartney" },
  188. { "firstName": "George", "lastName": "Harrison" },
  189. { "firstName": "Ringo", "lastName": "Starr" }
  190. ],
  191. "name": function () {
  192. return this.firstName + " " + this.lastName;
  193. }
  194. }
  195. ```
  196. Template:
  197. ```html
  198. {{#beatles}}
  199. * {{name}}
  200. {{/beatles}}
  201. ```
  202. Output:
  203. ```html
  204. * John Lennon
  205. * Paul McCartney
  206. * George Harrison
  207. * Ringo Starr
  208. ```
  209. #### Functions
  210. 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.
  211. View:
  212. ```js
  213. {
  214. "name": "Tater",
  215. "bold": function () {
  216. return function (text, render) {
  217. return "<b>" + render(text) + "</b>";
  218. }
  219. }
  220. }
  221. ```
  222. Template:
  223. ```html
  224. {{#bold}}Hi {{name}}.{{/bold}}
  225. ```
  226. Output:
  227. ```html
  228. <b>Hi Tater.</b>
  229. ```
  230. ### Inverted Sections
  231. 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.
  232. View:
  233. ```json
  234. {
  235. "repos": []
  236. }
  237. ```
  238. Template:
  239. ```html
  240. {{#repos}}<b>{{name}}</b>{{/repos}}
  241. {{^repos}}No repos :({{/repos}}
  242. ```
  243. Output:
  244. ```html
  245. No repos :(
  246. ```
  247. ### Comments
  248. Comments begin with a bang and are ignored. The following template:
  249. ```html
  250. <h1>Today{{! ignore me }}.</h1>
  251. ```
  252. Will render as follows:
  253. ```html
  254. <h1>Today.</h1>
  255. ```
  256. Comments may contain newlines.
  257. ### Partials
  258. Partials begin with a greater than sign, like {{> box}}.
  259. Partials are rendered at runtime (as opposed to compile time), so recursive partials are possible. Just avoid infinite loops.
  260. They also inherit the calling context. Whereas in ERB you may have this:
  261. ```html+erb
  262. <%= partial :next_more, :start => start, :size => size %>
  263. ```
  264. Mustache requires only this:
  265. ```html
  266. {{> next_more}}
  267. ```
  268. 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.
  269. For example, this template and partial:
  270. base.mustache:
  271. <h2>Names</h2>
  272. {{#names}}
  273. {{> user}}
  274. {{/names}}
  275. user.mustache:
  276. <strong>{{name}}</strong>
  277. Can be thought of as a single, expanded template:
  278. ```html
  279. <h2>Names</h2>
  280. {{#names}}
  281. <strong>{{name}}</strong>
  282. {{/names}}
  283. ```
  284. 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.
  285. ```js
  286. Mustache.render(template, view, {
  287. user: userTemplate
  288. });
  289. ```
  290. ### Set Delimiter
  291. Set Delimiter tags start with an equals sign and change the tag delimiters from `{{` and `}}` to custom strings.
  292. Consider the following contrived example:
  293. ```
  294. * {{ default_tags }}
  295. {{=<% %>=}}
  296. * <% erb_style_tags %>
  297. <%={{ }}=%>
  298. * {{ default_tags_again }}
  299. ```
  300. 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.
  301. 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."
  302. Custom delimiters may not contain whitespace or the equals sign.
  303. ## Pre-parsing and Caching Templates
  304. By default, when mustache.js first parses a template it keeps the full parsed token tree in a cache. The next time it sees that same template it skips the parsing step and renders the template much more quickly. If you'd like, you can do this ahead of time using `mustache.parse`.
  305. ```js
  306. Mustache.parse(template);
  307. // Then, sometime later.
  308. Mustache.render(template, view);
  309. ```
  310. ## Plugins for JavaScript Libraries
  311. mustache.js may be built specifically for several different client libraries, including the following:
  312. - [jQuery](http://jquery.com/)
  313. - [MooTools](http://mootools.net/)
  314. - [Dojo](http://www.dojotoolkit.org/)
  315. - [YUI](http://developer.yahoo.com/yui/)
  316. - [qooxdoo](http://qooxdoo.org/)
  317. These may be built using [Rake](http://rake.rubyforge.org/) and one of the following commands:
  318. $ rake jquery
  319. $ rake mootools
  320. $ rake dojo
  321. $ rake yui3
  322. $ rake qooxdoo
  323. ## Testing
  324. 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/).
  325. $ npm install -g mocha
  326. You also need to install the sub module containing [Mustache specifications](http://github.com/mustache/spec) in the project root.
  327. $ git submodule init
  328. $ git submodule update
  329. Then run the tests.
  330. $ mocha test
  331. 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:
  332. 1. Create a template file named `mytest.mustache` in the `test/_files`
  333. directory. Replace `mytest` with the name of your test.
  334. 2. Create a corresponding view file named `mytest.js` in the same directory.
  335. This file should contain a JavaScript object literal enclosed in
  336. parentheses. See any of the other view files for an example.
  337. 3. Create a file with the expected output in `mytest.txt` in the same
  338. directory.
  339. Then, you can run the test with:
  340. $ TEST=mytest mocha test/render-test.js
  341. ## Thanks
  342. mustache.js wouldn't kick ass if it weren't for these fine souls:
  343. * Chris Wanstrath / defunkt
  344. * Alexander Lang / langalex
  345. * Sebastian Cohnen / tisba
  346. * J Chris Anderson / jchris
  347. * Tom Robinson / tlrobinson
  348. * Aaron Quint / quirkey
  349. * Douglas Crockford
  350. * Nikita Vasilyev / NV
  351. * Elise Wood / glytch
  352. * Damien Mathieu / dmathieu
  353. * Jakub Kuźma / qoobaa
  354. * Will Leinweber / will
  355. * dpree
  356. * Jason Smith / jhs
  357. * Aaron Gibralter / agibralter
  358. * Ross Boucher / boucher
  359. * Matt Sanford / mzsanford
  360. * Ben Cherry / bcherry
  361. * Michael Jackson / mjijackson