Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 14 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 16 gadiem
pirms 14 gadiem
pirms 14 gadiem
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.
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
pirms 14 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 mustache templates anywhere you can use
  15. JavaScript. This includes web browsers, server-side environments such as [node](http://nodejs.org/), and [CouchDB](http://couchdb.apache.org/)
  16. views.
  17. mustache.js ships with support for both the [CommonJS](http://www.commonjs.org/)
  18. module API and the [Asynchronous Module Definition](https://github.com/amdjs/amdjs-api/wiki/AMD) API, or AMD.
  19. ## Who uses mustache.js?
  20. An updated list of mustache.js users is kept [on the Github wiki](http://wiki.github.com/janl/mustache.js/beard-competition).
  21. Add yourself or your company if you use mustache.js!
  22. ## Usage
  23. Below is quick example how to use mustache.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. In this example, the `Mustache.render` function takes two parameters: 1) the
  32. [mustache](http://mustache.github.com/) template and 2) a `view` object that
  33. contains the data and code needed to render the template.
  34. ## Templates
  35. A [mustache](http://mustache.github.com/) template is a string that contains
  36. any number of mustache tags. Tags are indicated by the double mustaches that
  37. surround them. `{{person}}` is a tag, as is `{{#person}}`. In both examples we
  38. refer to `person` as the tag's key.
  39. There are several types of tags available in mustache.js.
  40. ### Variables
  41. The most basic tag type is a simple variable. A `{{name}}` tag renders the value
  42. of the `name` key in the current context. If there is no such key, nothing is
  43. rendered.
  44. All variables are HTML-escaped by default. If you want to render unescaped HTML,
  45. use the triple mustache: `{{{name}}}`. You can also use `&` to unescape a
  46. variable.
  47. Template:
  48. * {{name}}
  49. * {{age}}
  50. * {{company}}
  51. * {{{company}}}
  52. * {{&company}}
  53. View:
  54. {
  55. "name": "Chris",
  56. "company": "<b>GitHub</b>"
  57. }
  58. Output:
  59. * Chris
  60. *
  61. * &lt;b&gt;GitHub&lt;/b&gt;
  62. * <b>GitHub</b>
  63. * <b>GitHub</b>
  64. JavaScript's dot notation may be used to access keys that are properties of
  65. objects in a view.
  66. Template:
  67. * {{name.first}} {{name.last}}
  68. * {{age}}
  69. View:
  70. {
  71. "name": {
  72. "first": "Michael",
  73. "last": "Jackson"
  74. },
  75. "age": "RIP"
  76. }
  77. Output:
  78. * Michael Jackson
  79. * RIP
  80. ### Sections
  81. Sections render blocks of text one or more times, depending on the value of the
  82. key in the current context.
  83. A section begins with a pound and ends with a slash. That is, `{{#person}}`
  84. begins a `person` section, while `{{/person}}` ends it. The text between the two
  85. tags is referred to as that section's "block".
  86. The behavior of the section is determined by the value of the key.
  87. #### False Values or Empty Lists
  88. If the `person` key exists and has a value of `null`, `undefined`, or `false`,
  89. or is an empty list, the block will not be rendered.
  90. Template:
  91. Shown.
  92. {{#person}}
  93. Never shown!
  94. {{/person}}
  95. View:
  96. {
  97. "person": false
  98. }
  99. Output:
  100. Shown.
  101. #### Non-Empty Lists
  102. If the `person` key exists and is not `null`, `undefined`, or `false`, and is
  103. not an empty list the block will be rendered one or more times.
  104. When the value is a list, the block is rendered once for each item in the list.
  105. The context of the block is set to the current item in the list for each
  106. iteration. In this way we can loop over collections.
  107. Template:
  108. {{#stooges}}
  109. <b>{{name}}</b>
  110. {{/stooges}}
  111. View:
  112. {
  113. "stooges": [
  114. { "name": "Moe" },
  115. { "name": "Larry" },
  116. { "name": "Curly" }
  117. ]
  118. }
  119. Output:
  120. <b>Moe</b>
  121. <b>Larry</b>
  122. <b>Curly</b>
  123. When looping over an array of strings, a `.` can be used to refer to the current
  124. item in the list.
  125. Template:
  126. {{#musketeers}}
  127. * {{.}}
  128. {{/musketeers}}
  129. View:
  130. {
  131. "musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"]
  132. }
  133. Output:
  134. * Athos
  135. * Aramis
  136. * Porthos
  137. * D'Artagnan
  138. If the value of a section variable is a function, it will be called in the
  139. context of the current item in the list on each iteration.
  140. Template:
  141. {{#beatles}}
  142. * {{name}}
  143. {{/beatles}}
  144. View:
  145. {
  146. "beatles": [
  147. { "firstName": "John", "lastName": "Lennon" },
  148. { "firstName": "Paul", "lastName": "McCartney" },
  149. { "firstName": "George", "lastName": "Harrison" },
  150. { "firstName": "Ringo", "lastName": "Starr" }
  151. ],
  152. "name": function () {
  153. return this.firstName + " " + this.lastName;
  154. }
  155. }
  156. Output:
  157. * John Lennon
  158. * Paul McCartney
  159. * George Harrison
  160. * Ringo Starr
  161. #### Functions
  162. If the value of a section key is a function, it is called with the section's
  163. literal block of text, un-rendered, as its first argument. The second argument
  164. is a special rendering function that uses the current view as its view argument.
  165. It is called in the context of the current view object.
  166. Template:
  167. {{#bold}}Hi {{name}}.{{/bold}}
  168. View:
  169. {
  170. "name": "Tater",
  171. "bold": function () {
  172. return function (text, render) {
  173. return "<b>" + render(text) + "</b>";
  174. }
  175. }
  176. }
  177. Output:
  178. <b>Hi Tater.</b>
  179. ### Inverted Sections
  180. An inverted section opens with `{{^section}}` instead of `{{#section}}`. The
  181. block of an inverted section is rendered only if the value of that section's tag
  182. is `null`, `undefined`, `false`, or an empty list.
  183. Template:
  184. {{#repos}}<b>{{name}}</b>{{/repos}}
  185. {{^repos}}No repos :({{/repos}}
  186. View:
  187. {
  188. "repos": []
  189. }
  190. Output:
  191. No repos :(
  192. ### Comments
  193. Comments begin with a bang and are ignored. The following template:
  194. <h1>Today{{! ignore me }}.</h1>
  195. Will render as follows:
  196. <h1>Today.</h1>
  197. Comments may contain newlines.
  198. ### Partials
  199. Partials begin with a greater than sign, like {{> box}}.
  200. Partials are rendered at runtime (as opposed to compile time), so recursive
  201. partials are possible. Just avoid infinite loops.
  202. They also inherit the calling context. Whereas in ERB you may have this:
  203. <%= partial :next_more, :start => start, :size => size %>
  204. Mustache requires only this:
  205. {{> next_more}}
  206. Why? Because the `next_more.mustache` file will inherit the `size` and `start`
  207. variables from the calling context. In this way you may want to think of
  208. partials as includes, or template expansion, even though it's not literally true.
  209. For example, this template and partial:
  210. base.mustache:
  211. <h2>Names</h2>
  212. {{#names}}
  213. {{> user}}
  214. {{/names}}
  215. user.mustache:
  216. <strong>{{name}}</strong>
  217. Can be thought of as a single, expanded template:
  218. <h2>Names</h2>
  219. {{#names}}
  220. <strong>{{name}}</strong>
  221. {{/names}}
  222. In mustache.js an object of partials may be passed as the third argument to
  223. `Mustache.render`. The object should be keyed by the name of the partial, and
  224. its value should be the partial text.
  225. ### Set Delimiter
  226. Set Delimiter tags start with an equals sign and change the tag delimiters from
  227. `{{` and `}}` to custom strings.
  228. Consider the following contrived example:
  229. * {{ default_tags }}
  230. {{=<% %>=}}
  231. * <% erb_style_tags %>
  232. <%={{ }}=%>
  233. * {{ default_tags_again }}
  234. Here we have a list with three items. The first item uses the default tag style,
  235. the second uses ERB style as defined by the Set Delimiter tag, and the third
  236. returns to the default style after yet another Set Delimiter declaration.
  237. According to [ctemplates](http://google-ctemplate.googlecode.com/svn/trunk/doc/howto.html),
  238. this "is useful for languages like TeX, where double-braces may occur in the
  239. text and are awkward to use for markup."
  240. Custom delimiters may not contain whitespace or the equals sign.
  241. ## Plugins for JavaScript Libraries
  242. mustache.js may be built specifically for several different client libraries,
  243. including the following:
  244. - [jQuery](http://jquery.com/)
  245. - [MooTools](http://mootools.net/)
  246. - [Dojo](http://www.dojotoolkit.org/)
  247. - [YUI](http://developer.yahoo.com/yui/)
  248. - [qooxdoo](http://qooxdoo.org/)
  249. These may be built using [Rake](http://rake.rubyforge.org/) and one of the
  250. following commands:
  251. $ rake jquery
  252. $ rake mootools
  253. $ rake dojo
  254. $ rake yui
  255. $ rake qooxdoo
  256. ## Testing
  257. The mustache.js test suite uses the [vows](http://vowsjs.org/) testing
  258. framework. In order to run the tests you'll need to install [node](http://nodejs.org/).
  259. Once that's done you can install vows using [npm](http://npmjs.org/).
  260. $ npm install -g vows
  261. Then run the tests.
  262. $ vows --spec
  263. The test suite consists of both unit and integration tests. If a template isn't
  264. rendering correctly for you, you can make a test for it by doing the following:
  265. 1. Create a template file named `mytest.mustache` in the `test/_files`
  266. directory. Replace `mytest` with the name of your test.
  267. 2. Create a corresponding view file named `mytest.js` in the same directory.
  268. This file should contain a JavaScript object literal enclosed in
  269. parentheses. See any of the other view files for an example.
  270. 3. Create a file with the expected output in `mytest.txt` in the same
  271. directory.
  272. Then, you can run the test with:
  273. $ TEST=mytest vows test/render_test.js
  274. ## Thanks
  275. mustache.js wouldn't kick ass if it weren't for these fine souls:
  276. * Chris Wanstrath / defunkt
  277. * Alexander Lang / langalex
  278. * Sebastian Cohnen / tisba
  279. * J Chris Anderson / jchris
  280. * Tom Robinson / tlrobinson
  281. * Aaron Quint / quirkey
  282. * Douglas Crockford
  283. * Nikita Vasilyev / NV
  284. * Elise Wood / glytch
  285. * Damien Mathieu / dmathieu
  286. * Jakub Kuźma / qoobaa
  287. * Will Leinweber / will
  288. * dpree
  289. * Jason Smith / jhs
  290. * Aaron Gibralter / agibralter
  291. * Ross Boucher / boucher
  292. * Matt Sanford / mzsanford
  293. * Ben Cherry / bcherry
  294. * Michael Jackson / mjijackson