Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 15 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 15 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
před 16 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. # mustache.js — Logic-less templates with JavaScript
  2. > What could be more logical awesome than no logic at all?
  3. For a list of implementations (other than JavaScript) and editor
  4. plugins, see <http://mustache.github.com/>.
  5. ## Where to Use?
  6. You can use mustache.js rendering stuff in various scenarios. E.g. you can
  7. render templates in your browser, or rendering server-side stuff with
  8. [node.js][node.js], use it for rendering stuff in [CouchDB][couchdb]’s views.
  9. ## Who Uses Mustache?
  10. An updated list is kept on the Github wiki. Add yourself, if you use
  11. mustache.js: <http://wiki.github.com/janl/mustache.js/beard-competition>
  12. ## Usage
  13. A quick example how to use mustache.js:
  14. var view = {
  15. title: "Joe",
  16. calc: function() {
  17. return 2 + 4;
  18. }
  19. }
  20. var template = "{{title}} spends {{calc}}";
  21. var html = Mustache.to_html(template, view);
  22. `template` is a simple string with mustache tags and `view` is a JavaScript
  23. object containing the data and any code to render the template.
  24. ## Template Tag Types
  25. There are several types of tags currently implemented in mustache.js.
  26. For a language-agnostic overview of Mustache’s template syntax, see the
  27. `mustache(5)` manpage or <http://mustache.github.com/mustache.5.html>.
  28. ### Simple Tags
  29. Tags are always surrounded by mustaches like this `{{foobar}}`.
  30. var view = {name: "Joe", say_hello: function(){ return "hello" }}
  31. template = "{{say_hello}}, {{name}}"
  32. #### Accessing values in nested objects (Dot Notation)
  33. To access data logically grouped into nested objects, specify a '.' delimited
  34. path to the value.
  35. var contact = {
  36. name: {first: "Bill", last: "Bobitybob" },
  37. age: 37
  38. }
  39. template = "Hello, {{name.first}} {{name.last}}. You are {{age}} years old."
  40. *NOTICE*: The dot notation feature was recently implemented for the 0.4
  41. release, which is not out as of Nov 9 2011. You can find the feature in the
  42. current master branch of mustachejs.
  43. ### Conditional Sections
  44. Conditional sections begin with `{{#condition}}` and end with
  45. `{{/condition}}`. When `condition` evaluates to true, the section is rendered,
  46. otherwise the whole block will output nothing at all. `condition` may be a
  47. function returning true/false or a simple boolean.
  48. var view = {condition: function() {
  49. // [...your code goes here...]
  50. return true;
  51. }}
  52. {{#condition}}
  53. I will be visible if condition is true
  54. {{/condition}}
  55. ### Enumerable Sections
  56. Enumerable Sections use the same syntax as condition sections do.
  57. `{{#shopping_items}}` and `{{/shopping_items}}`. Actually the view decides how
  58. mustache.js renders the section. If the view returns an array, it will
  59. iterator over the items. Use `{{.}}` to access the current item inside the
  60. enumeration section.
  61. var view = {name: "Joe's shopping card",
  62. items: ["bananas", "apples"]}
  63. var template = "{{name}}: <ul> {{#items}}<li>{{.}}</li>{{/items}} </ul>"
  64. Outputs:
  65. Joe's shopping card: <ul><li>bananas</li><li>apples</li></ul>
  66. ### Higher Order Sections
  67. If a section key returns a function, it will be called and passed both the
  68. unrendered block of text and a renderer convenience function.
  69. Given this object:
  70. "name": "Tater",
  71. "bolder": function() {
  72. return function(text, render) {
  73. return "<b>" + render(text) + '</b>'
  74. }
  75. }
  76. And this template:
  77. {{#bolder}}Hi {{name}}.{{/bolder}}
  78. We'll get this output:
  79. <b>Hi Tater.</b>
  80. As you can see, we’re pre-processing the text in the block. This can be used
  81. to implement caching, filters (like syntax highlighting), etc.
  82. You can use `this.name` to access the attribute `name` from your view.
  83. ### Dereferencing Sections
  84. If your data has components that are logically grouped into nested objects,
  85. you may wish to dereference an object to access its values.
  86. Given this object:
  87. {
  88. "name": "Bill",
  89. "address": {
  90. "street": "801 Streetly street",
  91. "city": "Boston",
  92. "state": "MA",
  93. "zip" "02101"
  94. }
  95. }
  96. And this template:
  97. <h1>Contact: {{name}}</h1>
  98. {{#address}}
  99. <p>{{street}}</p>
  100. <p>{{city}}, {{state}} {{zip}}</p>
  101. {{/address}}
  102. We'll get this output:
  103. <h1>Contact: Bill</h1>
  104. <p>801 Streetly street</p>
  105. <p>Boston, MA 02101</p>
  106. ### Inverted Sections
  107. An inverted section opens with `{{^section}}` instead of `{{#section}}` and
  108. uses a boolean negative to evaluate. Empty arrays are considered falsy.
  109. View:
  110. var inverted_section = {
  111. "repo": []
  112. }
  113. Template:
  114. {{#repo}}<b>{{name}}</b>{{/repo}}
  115. {{^repo}}No repos :({{/repo}}
  116. Result:
  117. No repos :(
  118. ### View Partials
  119. mustache.js supports a quite powerful but yet simple view partial mechanism.
  120. Use the following syntax for partials: `{{>partial_name}}`
  121. var view = {
  122. name: "Joe",
  123. winnings: {
  124. value: 1000,
  125. taxed_value: function() {
  126. return this.value - (this.value * 0.4);
  127. }
  128. }
  129. };
  130. var template = "Welcome, {{name}}! {{>winnings}}"
  131. var partials = {
  132. winnings: "You just won ${{value}} (which is ${{taxed_value}} after tax)"};
  133. var output = Mustache.to_html(template, view, partials)
  134. output will be:
  135. Welcome, Joe! You just won $1000 (which is $600 after tax)
  136. You invoke a partial with `{{>winnings}}`. Invoking the partial `winnings`
  137. will tell mustache.js to look for a object in the context's property
  138. `winnings`. It will then use that object as the context for the template found
  139. in `partials` for `winnings`.
  140. ## Escaping
  141. mustache.js does escape all values when using the standard double mustache
  142. syntax. Characters which will be escaped: `& \ " ' < >`. To disable escaping,
  143. simply use triple mustaches like `{{{unescaped_variable}}}`.
  144. Example: Using `{{variable}}` inside a template for `5 > 2` will result in `5 &gt; 2`, where as the usage of `{{{variable}}}` will result in `5 > 2`.
  145. ## Streaming
  146. To stream template results out of mustache.js, you can pass an optional
  147. `send()` callback to the `to_html()` call:
  148. Mustache.to_html(template, view, partials, function(line) {
  149. print(line);
  150. });
  151. ## Pragmas
  152. Pragma tags let you alter the behaviour of mustache.js. They have the format
  153. of
  154. {{%PRAGMANAME}}
  155. and they accept options:
  156. {{%PRAGMANAME option=value}}
  157. ### IMPLICIT-ITERATOR
  158. When using a block to iterate over an enumerable (Array), mustache.js expects
  159. an objects as enumerable items. The implicit iterator pragma enables optional
  160. behaviour of allowing literals as enumerable items. Consider this view:
  161. var view = {
  162. foo: [1, 2, 3, 4, 5, "french"]
  163. };
  164. The following template can iterate over the member `foo`:
  165. {{%IMPLICIT-ITERATOR}}
  166. {{#foo}}
  167. {{.}}
  168. {{/foo}}
  169. If you don't like the dot in there, the pragma accepts an option to set your
  170. own iteration marker:
  171. {{%IMPLICIT-ITERATOR iterator=bob}}
  172. {{#foo}}
  173. {{bob}}
  174. {{/foo}}
  175. ## More Examples and Documentation
  176. See `examples/` for more goodies and read the [original mustache docs][m]
  177. ## Command Line
  178. See `mustache(1)` man page or
  179. <http://defunkt.github.com/mustache/mustache.1.html>
  180. for command line docs.
  181. Or just install it as a RubyGem:
  182. $ gem install mustache
  183. $ mustache -h
  184. [m]: http://github.com/defunkt/mustache/#readme
  185. [node.js]: http://nodejs.org
  186. [couchdb]: http://couchdb.apache.org
  187. ## Plugins for jQuery, Dojo, Yui, CommonJS, qooxdoo
  188. This repository lets you build modules for [jQuery][], [Dojo][], [Yui][] and
  189. [CommonJS][] / [Node.js][] with the help of `rake`. You may need to install
  190. rspec first by running `gem install rspec`.
  191. Run `rake jquery` to get a jQuery compatible plugin file in the
  192. `mustache-jquery/` directory.
  193. Run `rake dojo` to get a Dojo compatible plugin file in the `mustache-dojo/`
  194. directory.
  195. Run `rake yui` to get a Yui compatible plugin file in the `mustache-yui/`
  196. directory.
  197. Run `rake commonjs` to get a CommonJS compatible plugin file in the
  198. `mustache-commonjs/` directory which you can also use with [Node.js][].
  199. Run `rake qooxdoo` to get a qooxdoo compatible file named `qooxdoo.mustache.js`.
  200. ## Testing
  201. To run the mustache.js test suite, run `rake spec`. All specs will be run first with JavaScriptCore (using `jsc`)
  202. and again with Rhino, using `java org.mozilla.javascript.tools.shell.Main`.
  203. JavaScriptCore is used from the OSX default location:
  204. /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
  205. To install Rhino on OSX, follow [these instructions](Rhino Install).
  206. ### Adding Tests
  207. Tests are located in the `examples/` directory. Adding a new test requires three files. Here's an example to add a test named "foo":
  208. `examples/foo.html` (the template):
  209. foo {{bar}}
  210. `examples/foo.js` (the view context):
  211. var foo = {
  212. bar: "baz"
  213. };
  214. `examples/foo.txt` (the expected output):
  215. foo baz
  216. [jQuery]: http://jquery.com/
  217. [Dojo]: http://www.dojotoolkit.org/
  218. [Yui]: http://developer.yahoo.com/yui/
  219. [CommonJS]: http://www.commonjs.org/
  220. [Node.js]: http://nodejs.org/
  221. [Rhino Install]: http://michaux.ca/articles/installing-rhino-on-os-x