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.

14 vuotta sitten
16 vuotta sitten
11 vuotta sitten
13 vuotta sitten
13 vuotta sitten
16 vuotta sitten
13 vuotta sitten
16 vuotta sitten
13 vuotta sitten
14 vuotta sitten
16 vuotta sitten
13 vuotta sitten
14 vuotta sitten
13 vuotta sitten
16 vuotta sitten
14 vuotta sitten
16 vuotta sitten
13 vuotta sitten
16 vuotta sitten
11 vuotta sitten
11 vuotta sitten
16 vuotta sitten
16 vuotta sitten
16 vuotta sitten
16 vuotta sitten
13 vuotta sitten
14 vuotta sitten
16 vuotta sitten
16 vuotta sitten
14 vuotta sitten
16 vuotta sitten
13 vuotta sitten
13 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
13 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
13 vuotta sitten
14 vuotta sitten
13 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
13 vuotta sitten
14 vuotta sitten
13 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
13 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
13 vuotta sitten
14 vuotta sitten
12 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
13 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
11 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
16 vuotta sitten
14 vuotta sitten
14 vuotta sitten
16 vuotta sitten
14 vuotta sitten
16 vuotta sitten
13 vuotta sitten
14 vuotta sitten
16 vuotta sitten
14 vuotta sitten
16 vuotta sitten
14 vuotta sitten
16 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
16 vuotta sitten
13 vuotta sitten
16 vuotta sitten
16 vuotta sitten
13 vuotta sitten
16 vuotta sitten
14 vuotta sitten
16 vuotta sitten
16 vuotta sitten
13 vuotta sitten
16 vuotta sitten
16 vuotta sitten
14 vuotta sitten
16 vuotta sitten
14 vuotta sitten
13 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
13 vuotta sitten
14 vuotta sitten
13 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
10 vuotta sitten
11 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. # mustache.js - Logic-less {{mustache}} templates with JavaScript
  2. > What could be more logical awesome than no logic at all?
  3. [![Build Status](https://travis-ci.org/janl/mustache.js.svg?branch=master)](https://travis-ci.org/janl/mustache.js) [![Gitter chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/janl/mustache.js)
  4. [mustache.js](http://github.com/janl/mustache.js) is an implementation of the [mustache](http://mustache.github.com/) template system in JavaScript.
  5. [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.
  6. 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.
  7. For a language-agnostic overview of mustache's template syntax, see the `mustache(5)` [manpage](http://mustache.github.com/mustache.5.html).
  8. ## Where to use mustache.js?
  9. 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.
  10. 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.
  11. And this will be your templates after you use Mustache:
  12. !['stache](https://cloud.githubusercontent.com/assets/288977/8779228/a3cf700e-2f02-11e5-869a-300312fb7a00.gif)
  13. ## Install
  14. You can get Mustache via npm.
  15. ```bash
  16. $ npm install mustache --save
  17. ```
  18. or install with bower:
  19. ```bash
  20. $ bower install --save mustache
  21. ```
  22. ## Command line tool
  23. mustache.js is shipped with a node based command line tool. It might be installed as a global tool on your computer to render a mustache template of some kind
  24. ```bash
  25. $ npm install -g mustache
  26. $ mustache dataView.json myTemplate.mustache > output.html
  27. ```
  28. also supports stdin.
  29. ```bash
  30. $ cat dataView.json | mustache - myTemplate.mustache > output.html
  31. ```
  32. or as a package.json `devDependency` in a build process maybe?
  33. ```bash
  34. $ npm install mustache --save-dev
  35. ```
  36. ```json
  37. {
  38. "scripts": {
  39. "build": "mustache dataView.json myTemplate.mustache > public/output.html"
  40. }
  41. }
  42. ```
  43. ```bash
  44. $ npm run build
  45. ```
  46. The command line tool is basically a wrapper around `Mustache.render` so you get all the features.
  47. If your templates use partials you should pass paths to partials using `-p` flag:
  48. ```bash
  49. $ mustache -p path/to/partial1.mustache -p path/to/partial2.mustache dataView.json myTemplate.mustache
  50. ```
  51. ## Who uses mustache.js?
  52. 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!
  53. ## Contributing
  54. 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.
  55. ### Your First Contribution
  56. - review a [Pull Request](https://github.com/janl/mustache.js/pulls)
  57. - fix an [Issue](https://github.com/janl/mustache.js/issues)
  58. - update the [documentation](https://github.com/janl/mustache.js#usage)
  59. - make a website
  60. - write a tutorial
  61. * * *
  62. ## Usage
  63. Below is a quick example how to use mustache.js:
  64. ```js
  65. var view = {
  66. title: "Joe",
  67. calc: function () {
  68. return 2 + 4;
  69. }
  70. };
  71. var output = Mustache.render("{{title}} spends {{calc}}", view);
  72. ```
  73. 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.
  74. ## API
  75. Following is an [rtype](https://git.io/rtype) signature of the most commonly used functions.
  76. ```js
  77. Mustache.render(
  78. template : String,
  79. view : Object,
  80. partials? : Object,
  81. ) => String
  82. Mustache.parse(
  83. template : String,
  84. tags = ['{{', '}}'] : Tags,
  85. ) => String
  86. interface Tags [String, String]
  87. ```
  88. ## Templates
  89. 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.
  90. There are several techniques that can be used to load templates and hand them to mustache.js, here are two of them:
  91. #### Include Templates
  92. 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`:
  93. ```html
  94. <!DOCTYPE HTML>
  95. <html>
  96. <body onload="loadUser()">
  97. <div id="target">Loading...</div>
  98. <script id="template" type="x-tmpl-mustache">
  99. Hello {{ name }}!
  100. </script>
  101. </body>
  102. </html>
  103. ```
  104. ```js
  105. function loadUser() {
  106. var template = $('#template').html();
  107. Mustache.parse(template); // optional, speeds up future uses
  108. var rendered = Mustache.render(template, {name: "Luke"});
  109. $('#target').html(rendered);
  110. }
  111. ```
  112. #### Load External Templates
  113. If your templates reside in individual files, you can load them asynchronously and render them when they arrive. Another example using `jQuery`:
  114. ```js
  115. function loadUser() {
  116. $.get('template.mst', function(template) {
  117. var rendered = Mustache.render(template, {name: "Luke"});
  118. $('#target').html(rendered);
  119. });
  120. }
  121. ```
  122. ### Variables
  123. 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.
  124. 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.
  125. If you want `{{name}}` _not_ to be interpreted as a mustache tag, but rather to appear exactly as `{{name}}` in the output, you must change and then restore the default delimiter. See the [Custom Delimiters](#custom-delimiters) section for more information.
  126. View:
  127. ```json
  128. {
  129. "name": "Chris",
  130. "company": "<b>GitHub</b>"
  131. }
  132. ```
  133. Template:
  134. ```
  135. * {{name}}
  136. * {{age}}
  137. * {{company}}
  138. * {{{company}}}
  139. * {{&company}}
  140. {{=<% %>=}}
  141. * {{company}}
  142. <%={{ }}=%>
  143. ```
  144. Output:
  145. ```html
  146. * Chris
  147. *
  148. * &lt;b&gt;GitHub&lt;/b&gt;
  149. * <b>GitHub</b>
  150. * <b>GitHub</b>
  151. * {{company}}
  152. ```
  153. JavaScript's dot notation may be used to access keys that are properties of objects in a view.
  154. View:
  155. ```json
  156. {
  157. "name": {
  158. "first": "Michael",
  159. "last": "Jackson"
  160. },
  161. "age": "RIP"
  162. }
  163. ```
  164. Template:
  165. ```html
  166. * {{name.first}} {{name.last}}
  167. * {{age}}
  168. ```
  169. Output:
  170. ```html
  171. * Michael Jackson
  172. * RIP
  173. ```
  174. ### Sections
  175. Sections render blocks of text one or more times, depending on the value of the key in the current context.
  176. 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".
  177. The behavior of the section is determined by the value of the key.
  178. #### False Values or Empty Lists
  179. 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.
  180. View:
  181. ```json
  182. {
  183. "person": false
  184. }
  185. ```
  186. Template:
  187. ```html
  188. Shown.
  189. {{#person}}
  190. Never shown!
  191. {{/person}}
  192. ```
  193. Output:
  194. ```html
  195. Shown.
  196. ```
  197. #### Non-Empty Lists
  198. 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.
  199. 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.
  200. View:
  201. ```json
  202. {
  203. "stooges": [
  204. { "name": "Moe" },
  205. { "name": "Larry" },
  206. { "name": "Curly" }
  207. ]
  208. }
  209. ```
  210. Template:
  211. ```html
  212. {{#stooges}}
  213. <b>{{name}}</b>
  214. {{/stooges}}
  215. ```
  216. Output:
  217. ```html
  218. <b>Moe</b>
  219. <b>Larry</b>
  220. <b>Curly</b>
  221. ```
  222. When looping over an array of strings, a `.` can be used to refer to the current item in the list.
  223. View:
  224. ```json
  225. {
  226. "musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"]
  227. }
  228. ```
  229. Template:
  230. ```html
  231. {{#musketeers}}
  232. * {{.}}
  233. {{/musketeers}}
  234. ```
  235. Output:
  236. ```html
  237. * Athos
  238. * Aramis
  239. * Porthos
  240. * D'Artagnan
  241. ```
  242. 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.
  243. View:
  244. ```js
  245. {
  246. "beatles": [
  247. { "firstName": "John", "lastName": "Lennon" },
  248. { "firstName": "Paul", "lastName": "McCartney" },
  249. { "firstName": "George", "lastName": "Harrison" },
  250. { "firstName": "Ringo", "lastName": "Starr" }
  251. ],
  252. "name": function () {
  253. return this.firstName + " " + this.lastName;
  254. }
  255. }
  256. ```
  257. Template:
  258. ```html
  259. {{#beatles}}
  260. * {{name}}
  261. {{/beatles}}
  262. ```
  263. Output:
  264. ```html
  265. * John Lennon
  266. * Paul McCartney
  267. * George Harrison
  268. * Ringo Starr
  269. ```
  270. #### Functions
  271. 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.
  272. View:
  273. ```js
  274. {
  275. "name": "Tater",
  276. "bold": function () {
  277. return function (text, render) {
  278. return "<b>" + render(text) + "</b>";
  279. }
  280. }
  281. }
  282. ```
  283. Template:
  284. ```html
  285. {{#bold}}Hi {{name}}.{{/bold}}
  286. ```
  287. Output:
  288. ```html
  289. <b>Hi Tater.</b>
  290. ```
  291. ### Inverted Sections
  292. 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`, *falsy* or an empty list.
  293. View:
  294. ```json
  295. {
  296. "repos": []
  297. }
  298. ```
  299. Template:
  300. ```html
  301. {{#repos}}<b>{{name}}</b>{{/repos}}
  302. {{^repos}}No repos :({{/repos}}
  303. ```
  304. Output:
  305. ```html
  306. No repos :(
  307. ```
  308. ### Comments
  309. Comments begin with a bang and are ignored. The following template:
  310. ```html
  311. <h1>Today{{! ignore me }}.</h1>
  312. ```
  313. Will render as follows:
  314. ```html
  315. <h1>Today.</h1>
  316. ```
  317. Comments may contain newlines.
  318. ### Partials
  319. Partials begin with a greater than sign, like {{> box}}.
  320. Partials are rendered at runtime (as opposed to compile time), so recursive partials are possible. Just avoid infinite loops.
  321. They also inherit the calling context. Whereas in ERB you may have this:
  322. ```html+erb
  323. <%= partial :next_more, :start => start, :size => size %>
  324. ```
  325. Mustache requires only this:
  326. ```html
  327. {{> next_more}}
  328. ```
  329. 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, imports, template expansion, nested templates, or subtemplates, even though those aren't literally the case here.
  330. For example, this template and partial:
  331. base.mustache:
  332. <h2>Names</h2>
  333. {{#names}}
  334. {{> user}}
  335. {{/names}}
  336. user.mustache:
  337. <strong>{{name}}</strong>
  338. Can be thought of as a single, expanded template:
  339. ```html
  340. <h2>Names</h2>
  341. {{#names}}
  342. <strong>{{name}}</strong>
  343. {{/names}}
  344. ```
  345. 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.
  346. ```js
  347. Mustache.render(template, view, {
  348. user: userTemplate
  349. });
  350. ```
  351. ### Custom Delimiters
  352. Custom delimiters can be used in place of `{{` and `}}` by setting the new values in JavaScript or in templates.
  353. #### Setting in JavaScript
  354. The `Mustache.tags` property holds an array consisting of the opening and closing tag values. Set custom values by passing a new array of tags to `parse()`, which gets honored over the default values, or by overriding the `tags` property itself:
  355. ```js
  356. var customTags = [ '<%', '%>' ];
  357. ```
  358. ##### Pass Value into Parse Method
  359. ```js
  360. Mustache.parse(template, customTags);
  361. ```
  362. ##### Override Tags Property
  363. ```js
  364. Mustache.tags = customTags;
  365. // Subsequent parse() and render() calls will use customTags
  366. ```
  367. #### Setting in Templates
  368. Set Delimiter tags start with an equals sign and change the tag delimiters from `{{` and `}}` to custom strings.
  369. Consider the following contrived example:
  370. ```html+erb
  371. * {{ default_tags }}
  372. {{=<% %>=}}
  373. * <% erb_style_tags %>
  374. <%={{ }}=%>
  375. * {{ default_tags_again }}
  376. ```
  377. 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.
  378. According to [ctemplates](https://htmlpreview.github.io/?https://raw.githubusercontent.com/OlafvdSpek/ctemplate/master/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."
  379. Custom delimiters may not contain whitespace or the equals sign.
  380. ## Pre-parsing and Caching Templates
  381. 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`.
  382. ```js
  383. Mustache.parse(template);
  384. // Then, sometime later.
  385. Mustache.render(template, view);
  386. ```
  387. ## Plugins for JavaScript Libraries
  388. mustache.js may be built specifically for several different client libraries, including the following:
  389. - [jQuery](http://jquery.com/)
  390. - [MooTools](http://mootools.net/)
  391. - [Dojo](http://www.dojotoolkit.org/)
  392. - [YUI](http://developer.yahoo.com/yui/)
  393. - [qooxdoo](http://qooxdoo.org/)
  394. These may be built using [Rake](http://rake.rubyforge.org/) and one of the following commands:
  395. ```bash
  396. $ rake jquery
  397. $ rake mootools
  398. $ rake dojo
  399. $ rake yui3
  400. $ rake qooxdoo
  401. ```
  402. ## Testing
  403. In order to run the tests you'll need to install [node](http://nodejs.org/).
  404. You also need to install the sub module containing [Mustache specifications](http://github.com/mustache/spec) in the project root.
  405. ```bash
  406. $ git submodule init
  407. $ git submodule update
  408. ```
  409. Install dependencies.
  410. ```bash
  411. $ npm install
  412. ```
  413. Then run the tests.
  414. ```bash
  415. $ npm test
  416. ```
  417. 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:
  418. 1. Create a template file named `mytest.mustache` in the `test/_files`
  419. directory. Replace `mytest` with the name of your test.
  420. 2. Create a corresponding view file named `mytest.js` in the same directory.
  421. This file should contain a JavaScript object literal enclosed in
  422. parentheses. See any of the other view files for an example.
  423. 3. Create a file with the expected output in `mytest.txt` in the same
  424. directory.
  425. Then, you can run the test with:
  426. ```bash
  427. $ TEST=mytest npm run test-render
  428. ```
  429. ### Browser tests
  430. Browser tests are not included in `npm test` as they run for too long, although they are ran automatically on Travis when merged into master. Run browser tests locally in any browser:
  431. ```bash
  432. $ npm run test-browser-local
  433. ```
  434. then point your browser to `http://localhost:8080/__zuul`
  435. ### Troubleshooting
  436. #### npm install fails
  437. Ensure to have a recent version of npm installed. While developing this project requires npm with support for `^` version ranges.
  438. ```bash
  439. $ npm install -g npm
  440. ```
  441. ## Thanks
  442. mustache.js wouldn't kick ass if it weren't for these fine souls:
  443. * Chris Wanstrath / defunkt
  444. * Alexander Lang / langalex
  445. * Sebastian Cohnen / tisba
  446. * J Chris Anderson / jchris
  447. * Tom Robinson / tlrobinson
  448. * Aaron Quint / quirkey
  449. * Douglas Crockford
  450. * Nikita Vasilyev / NV
  451. * Elise Wood / glytch
  452. * Damien Mathieu / dmathieu
  453. * Jakub Kuźma / qoobaa
  454. * Will Leinweber / will
  455. * dpree
  456. * Jason Smith / jhs
  457. * Aaron Gibralter / agibralter
  458. * Ross Boucher / boucher
  459. * Matt Sanford / mzsanford
  460. * Ben Cherry / bcherry
  461. * Michael Jackson / mjackson
  462. * Phillip Johnsen / phillipj
  463. * David da Silva Contín / dasilvacontin