You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 9.9KB

14 anni fa
16 anni fa
14 anni fa
Parser rewrite This commit is a complete rewrite of the core mustache.js file with two main goals: 1) a major performance boost and 2) better compliance with the mustache spec. In order to improve performance templates are pre-compiled to JavaScript functions. These compiled functions take a view, a partials object, and an optional callback as arguments. They are cached to prevent unnecessary re-compilation of an already compiled template. Both of these enhancements facilitate a generous boost in performance. A few other notes: - The mustache.js file is now both browser and CommonJS ready without any modification. - The API exposes two main methods: Mustache.compile and Mustache.render. The former is used to generate a function for a given template, while the latter is a higher-level function that is used to compile and render a template in one shot. Mustache.to_html is still available for backwards compatibility. - The concept of pragmas is removed to conform more closely to the original mustache spec. The dot symbol still works to reference the current item in an array. - The parser is much more strict about whitespace than it was before. The rule is simple: if a line contains only a non-variable tag (i.e. not {{tag}} or {{{tag}}}) and whitespace, that line is ignored in the output. Users may use the "space" option when compiling templates to preserve every whitespace character in the original template. - The parser is able to provide detailed information about where errors occur when parsing and rendering templates, including the line number and surrounding code context.
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
16 anni fa
14 anni fa
14 anni fa
14 anni fa
16 anni fa
14 anni fa
16 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
Parser rewrite This commit is a complete rewrite of the core mustache.js file with two main goals: 1) a major performance boost and 2) better compliance with the mustache spec. In order to improve performance templates are pre-compiled to JavaScript functions. These compiled functions take a view, a partials object, and an optional callback as arguments. They are cached to prevent unnecessary re-compilation of an already compiled template. Both of these enhancements facilitate a generous boost in performance. A few other notes: - The mustache.js file is now both browser and CommonJS ready without any modification. - The API exposes two main methods: Mustache.compile and Mustache.render. The former is used to generate a function for a given template, while the latter is a higher-level function that is used to compile and render a template in one shot. Mustache.to_html is still available for backwards compatibility. - The concept of pragmas is removed to conform more closely to the original mustache spec. The dot symbol still works to reference the current item in an array. - The parser is much more strict about whitespace than it was before. The rule is simple: if a line contains only a non-variable tag (i.e. not {{tag}} or {{{tag}}}) and whitespace, that line is ignored in the output. Users may use the "space" option when compiling templates to preserve every whitespace character in the original template. - The parser is able to provide detailed information about where errors occur when parsing and rendering templates, including the line number and surrounding code context.
14 anni fa
14 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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/) template 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 output = Mustache.render("{{title}} spends {{calc}}", view);
  30. In this example, the `Mustache.render` 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. ## Templates
  34. A [mustache](http://mustache.github.com/) template is a string that contains
  35. any number of mustache tags. Tags are indicated by the double mustaches that
  36. surround them. *{{person}}* is a tag, as is *{{#person}}*. In both examples we
  37. refer to *person* as the tag's key.
  38. There are several types of tags available in mustache.js.
  39. ### Variables
  40. The most basic tag type is a simple variable. A *{{name}}* tag renders the value
  41. of the *name* key in the current context. If there is no such key, nothing is
  42. rendered.
  43. All variables are HTML-escaped by default. If you want to render unescaped HTML,
  44. use the triple mustache: *{{{name}}}*. You can also use *&* to unescape a
  45. variable.
  46. Template:
  47. * {{name}}
  48. * {{age}}
  49. * {{company}}
  50. * {{{company}}}
  51. * {{&company}}
  52. View:
  53. {
  54. "name": "Chris",
  55. "company": "<b>GitHub</b>"
  56. }
  57. Output:
  58. * Chris
  59. *
  60. * &lt;b&gt;GitHub&lt;/b&gt;
  61. * <b>GitHub</b>
  62. * <b>GitHub</b>
  63. JavaScript's dot notation may be used to access keys that are properties of
  64. objects in a view.
  65. Template:
  66. * {{name.first}} {{name.last}}
  67. * {{age}}
  68. View:
  69. {
  70. "name": {
  71. "first": "Michael",
  72. "last": "Jackson"
  73. },
  74. "age": "RIP"
  75. }
  76. Output:
  77. * Michael Jackson
  78. * RIP
  79. ### Sections
  80. Sections render blocks of text one or more times, depending on the value of the
  81. key in the current context.
  82. A section begins with a pound and ends with a slash. That is, *{{#person}}*
  83. begins a *person* section, while *{{/person}}* ends it. The text between the two
  84. tags is referred to as that section's "block".
  85. The behavior of the section is determined by the value of the key.
  86. #### False Values or Empty Lists
  87. If the *person* key exists and has a value of `null`, `undefined`, or `false`,
  88. or is an empty list, the block will not be rendered.
  89. Template:
  90. Shown.
  91. {{#nothin}}
  92. Never shown!
  93. {{/nothin}}
  94. View:
  95. {
  96. "person": true
  97. }
  98. Output:
  99. Shown.
  100. #### Non-Empty Lists
  101. If the *person* key exists and is not `null`, `undefined`, or `false`, and is
  102. not an empty list the block will be rendered one or more times.
  103. When the value is a list, the block is rendered once for each item in the list.
  104. The context of the block is set to the current item in the list for each
  105. iteration. In this way we can loop over collections.
  106. Template:
  107. {{#stooges}}
  108. <b>{{name}}</b>
  109. {{/stooges}}
  110. View:
  111. {
  112. "stooges": [
  113. { "name": "Moe" },
  114. { "name": "Larry" },
  115. { "name": "Curly" }
  116. ]
  117. }
  118. Output:
  119. <b>Moe</b>
  120. <b>Larry</b>
  121. <b>Curly</b>
  122. When looping over an array of strings, a `.` can be used to refer to the current
  123. item in the list.
  124. Template:
  125. {{#musketeers}}
  126. * {{.}}
  127. {{/musketeers}}
  128. View:
  129. {
  130. "musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"]
  131. }
  132. Output:
  133. * Athos
  134. * Aramis
  135. * Porthos
  136. * D'Artagnan
  137. If the value of a section variable is a function, it will be called in the
  138. context of the current item in the list on each iteration.
  139. Template:
  140. {{#beatles}}
  141. * {{name}}
  142. {{/beatles}}
  143. View:
  144. {
  145. "beatles": [
  146. { "firstName": "John", "lastName": "Lennon" },
  147. { "firstName": "Paul", "lastName": "McCartney" },
  148. { "firstName": "George", "lastName": "Harrison" },
  149. { "firstName": "Ringo", "lastName": "Starr" }
  150. ],
  151. "name": function () {
  152. return this.firstName + " " + this.lastName;
  153. }
  154. }
  155. Output:
  156. * John Lennon
  157. * Paul McCartney
  158. * George Harrison
  159. * Ringo Starr
  160. #### Functions
  161. If the value of a section key is a function, it is called with the section's
  162. literal block of text, un-rendered, as its first argument. The second argument
  163. is a special rendering function that uses the current view as its view argument.
  164. It is called in the context of the current view object.
  165. Template:
  166. {{#bold}}Hi {{name}}.{{/bold}}
  167. View:
  168. {
  169. "name": "Tater",
  170. "bold": function () {
  171. return function (text, render) {
  172. return "<b>" + render(text) + "</b>";
  173. }
  174. }
  175. }
  176. Output:
  177. <b>Hi Tater.</b>
  178. ### Inverted Sections
  179. An inverted section opens with `{{^section}}` instead of `{{#section}}`. The
  180. block of an inverted section is rendered only if the value of that section's tag
  181. is `null`, `undefined`, `false`, or an empty list.
  182. Template:
  183. {{#repos}}<b>{{name}}</b>{{/repos}}
  184. {{^repos}}No repos :({{/repos}}
  185. View:
  186. {
  187. "repos": []
  188. }
  189. Output:
  190. No repos :(
  191. ### Comments
  192. Comments begin with a bang and are ignored. The following template:
  193. <h1>Today{{! ignore me }}.</h1>
  194. Will render as follows:
  195. <h1>Today.</h1>
  196. Comments may contain newlines.
  197. ### Partials
  198. Partials begin with a greater than sign, like {{> box}}.
  199. Partials are rendered at runtime (as opposed to compile time), so recursive
  200. partials are possible. Just avoid infinite loops.
  201. They also inherit the calling context. Whereas in ERB you may have this:
  202. <%= partial :next_more, :start => start, :size => size %>
  203. Mustache requires only this:
  204. {{> next_more}}
  205. Why? Because the `next_more.mustache` file will inherit the `size` and `start`
  206. variables from the calling context. In this way you may want to think of
  207. partials as includes, or template expansion, even though it's not literally true.
  208. For example, this template and partial:
  209. base.mustache:
  210. <h2>Names</h2>
  211. {{#names}}
  212. {{> user}}
  213. {{/names}}
  214. user.mustache:
  215. <strong>{{name}}</strong>
  216. Can be thought of as a single, expanded template:
  217. <h2>Names</h2>
  218. {{#names}}
  219. <strong>{{name}}</strong>
  220. {{/names}}
  221. In mustache.js an object of partials may be passed as the third argument to
  222. `Mustache.render`. The object should be keyed by the name of the partial, and
  223. its value should be the partial text.
  224. ### Set Delimiter
  225. Set Delimiter tags start with an equals sign and change the tag delimiters from
  226. `{{` and `}}` to custom strings.
  227. Consider the following contrived example:
  228. * {{ default_tags }}
  229. {{=<% %>=}}
  230. * <% erb_style_tags %>
  231. <%={{ }}=%>
  232. * {{ default_tags_again }}
  233. Here we have a list with three items. The first item uses the default tag style,
  234. the second uses ERB style as defined by the Set Delimiter tag, and the third
  235. returns to the default style after yet another Set Delimiter declaration.
  236. According to [ctemplates](http://google-ctemplate.googlecode.com/svn/trunk/doc/howto.html),
  237. this "is useful for languages like TeX, where double-braces may occur in the
  238. text and are awkward to use for markup."
  239. Custom delimiters may not contain whitespace or the equals sign.
  240. ## Streaming
  241. To stream template results out of mustache.js, you can pass an optional callback
  242. to the call to `Mustache.render`:
  243. Mustache.render(template, view, partials, function (chunk) {
  244. print(chunk);
  245. });
  246. When the template is finished rendering, the callback will be called with `null`
  247. after which it won't be called anymore for that rendering.
  248. ## Plugins for JavaScript Libraries
  249. By default mustache.js may be used in a browser or any [CommonJS](http://www.commonjs.org/)
  250. environment, including [node](http://nodejs.org/). Additionally, mustache.js may
  251. be built specifically for several different client libraries and platforms,
  252. including the following:
  253. - [jQuery](http://jquery.com/)
  254. - [Dojo](http://www.dojotoolkit.org/)
  255. - [YUI](http://developer.yahoo.com/yui/)
  256. - [RequireJS](http://requirejs.org/)
  257. - [qooxdoo](http://qooxdoo.org/)
  258. These may be built using [Rake](http://rake.rubyforge.org/) and one of the
  259. following commands:
  260. $ rake jquery
  261. $ rake dojo
  262. $ rake yui
  263. $ rake requirejs
  264. $ rake qooxdoo
  265. ## Thanks
  266. Mustache.js wouldn't kick ass if it weren't for these fine souls:
  267. * Chris Wanstrath / defunkt
  268. * Alexander Lang / langalex
  269. * Sebastian Cohnen / tisba
  270. * J Chris Anderson / jchris
  271. * Tom Robinson / tlrobinson
  272. * Aaron Quint / quirkey
  273. * Douglas Crockford
  274. * Nikita Vasilyev / NV
  275. * Elise Wood / glytch
  276. * Damien Mathieu / dmathieu
  277. * Jakub Kuźma / qoobaa
  278. * Will Leinweber / will
  279. * dpree
  280. * Jason Smith / jhs
  281. * Aaron Gibralter / agibralter
  282. * Ross Boucher / boucher
  283. * Matt Sanford / mzsanford
  284. * Ben Cherry / bcherry
  285. * Michael Jackson / mjijackson