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