Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 15 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 15 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 16 Jahren
vor 15 Jahren
vor 16 Jahren
vor 16 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. ### Conditional Sections
  33. Conditional sections begin with `{{#condition}}` and end with
  34. `{{/condition}}`. When `condition` evaluates to true, the section is rendered,
  35. otherwise the whole block will output nothing at all. `condition` may be a
  36. function returning true/false or a simple boolean.
  37. var view = {condition: function() {
  38. // [...your code goes here...]
  39. return true;
  40. }}
  41. {{#condition}}
  42. I will be visible if condition is true
  43. {{/condition}}
  44. ### Enumerable Sections
  45. Enumerable Sections use the same syntax as condition sections do.
  46. `{{#shopping_items}}` and `{{/shopping_items}}`. Actually the view decides how
  47. mustache.js renders the section. If the view returns an array, it will
  48. iterator over the items. Use `{{.}}` to access the current item inside the
  49. enumeration section.
  50. var view = {name: "Joe's shopping card",
  51. items: ["bananas", "apples"]}
  52. var template = "{{name}}: <ul> {{#items}}<li>{{.}}</li>{{/items}} </ul>"
  53. Outputs:
  54. Joe's shopping card: <ul><li>bananas</li><li>apples</li></ul>
  55. ### Higher Order Sections
  56. If a section key returns a function, it will be called and passed both the
  57. unrendered block of text and a renderer convenience function.
  58. Given this JS:
  59. "name": "Tater",
  60. "bolder": function() {
  61. return function(text, render) {
  62. return "<b>" + render(text) + '</b>'
  63. }
  64. }
  65. And this template:
  66. {{#bolder}}Hi {{name}}.{{/bolder}}
  67. We'll get this output:
  68. <b>Hi Tater.</b>
  69. As you can see, we’re pre-processing the text in the block. This can be used
  70. to implement caching, filters (like syntax highlighting), etc.
  71. You can use `this.name` to access the attribute `name` from your view.
  72. ### Dereferencing Section
  73. If you have a nested object structure in your view, it can sometimes be easier
  74. to use sections like this:
  75. var objects = {
  76. a_object: {
  77. title: 'this is an object',
  78. description: 'one of its attributes is a list',
  79. a_list: [{label: 'listitem1'}, {label: 'listitem2'}]
  80. }
  81. };
  82. This is our template:
  83. {{#a_object}}
  84. <h1>{{title}}</h1>
  85. <p>{{description}}</p>
  86. <ul>
  87. {{#a_list}}
  88. <li>{{label}}</li>
  89. {{/a_list}}
  90. </ul>
  91. {{/a_object}}
  92. Here is the result:
  93. <h1>this is an object</h1>
  94. <p>one of its attributes is a list</p>
  95. <ul>
  96. <li>listitem1</li>
  97. <li>listitem2</li>
  98. </ul>
  99. ### Inverted Sections
  100. An inverted section opens with `{{^section}}` instead of `{{#section}}` and
  101. uses a boolean negative to evaluate. Empty arrays are considered falsy.
  102. View:
  103. var inverted_section = {
  104. "repo": []
  105. }
  106. Template:
  107. {{#repo}}<b>{{name}}</b>{{/repo}}
  108. {{^repo}}No repos :({{/repo}}
  109. Result:
  110. No repos :(
  111. ### View Partials
  112. mustache.js supports a quite powerful but yet simple view partial mechanism.
  113. Use the following syntax for partials: `{{>partial_name}}`
  114. var view = {
  115. name: "Joe",
  116. winnings: {
  117. value: 1000,
  118. taxed_value: function() {
  119. return this.value - (this.value * 0.4);
  120. }
  121. }
  122. };
  123. var template = "Welcome, {{name}}! {{>winnings}}"
  124. var partials = {
  125. winnings: "You just won ${{value}} (which is ${{taxed_value}} after tax)"};
  126. var output = Mustache.to_html(template, view, partials)
  127. output will be:
  128. Welcome, Joe! You just won $1000 (which is $600 after tax)
  129. You invoke a partial with `{{>winnings}}`. Invoking the partial `winnings`
  130. will tell mustache.js to look for a object in the context's property
  131. `winnings`. It will then use that object as the context for the template found
  132. in `partials` for `winnings`.
  133. ## Escaping
  134. mustache.js does escape all values when using the standard double mustache
  135. syntax. Characters which will be escaped: `& \ " ' < >`. To disable escaping,
  136. simply use triple mustaches like `{{{unescaped_variable}}}`.
  137. 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`.
  138. ## Streaming
  139. To stream template results out of mustache.js, you can pass an optional
  140. `send()` callback to the `to_html()` call:
  141. Mustache.to_html(template, view, partials, function(line) {
  142. print(line);
  143. });
  144. ## Pragmas
  145. Pragma tags let you alter the behaviour of mustache.js. They have the format
  146. of
  147. {{%PRAGMANAME}}
  148. and they accept options:
  149. {{%PRAGMANAME option=value}}
  150. ### IMPLICIT-ITERATOR
  151. When using a block to iterate over an enumerable (Array), mustache.js expects
  152. an objects as enumerable items. The implicit iterator pragma enables optional
  153. behaviour of allowing literals as enumerable items. Consider this view:
  154. var view = {
  155. foo: [1, 2, 3, 4, 5, "french"]
  156. };
  157. The following template can iterate over the member `foo`:
  158. {{%IMPLICIT-ITERATOR}}
  159. {{#foo}}
  160. {{.}}
  161. {{/foo}}
  162. If you don't like the dot in there, the pragma accepts an option to set your
  163. own iteration marker:
  164. {{%IMPLICIT-ITERATOR iterator=bob}}
  165. {{#foo}}
  166. {{bob}}
  167. {{/foo}}
  168. ## F.A.Q.
  169. ### Why doesn’t Mustache allow dot notation like `{{variable.member}}`?
  170. The reason is given in the [mustache.rb
  171. bugtracker](http://github.com/defunkt/mustache/issues/issue/6).
  172. Mustache implementations strive to be template-compatible.
  173. ## More Examples and Documentation
  174. See `examples/` for more goodies and read the [original mustache docs][m]
  175. ## Command Line
  176. See `mustache(1)` man page or
  177. <http://defunkt.github.com/mustache/mustache.1.html>
  178. for command line docs.
  179. Or just install it as a RubyGem:
  180. $ gem install mustache
  181. $ mustache -h
  182. [m]: http://github.com/defunkt/mustache/#readme
  183. [node.js]: http://nodejs.org
  184. [couchdb]: http://couchdb.apache.org
  185. ## Plugins for jQuery, Dojo, Yui, CommonJS
  186. This repository lets you build modules for [jQuery][], [Dojo][], [Yui][] and
  187. [CommonJS][] / [Node.js][] with the help of `rake`. You may need to install
  188. rspec first by running `gem install rspec`.
  189. Run `rake jquery` to get a jQuery compatible plugin file in the
  190. `mustache-jquery/` directory.
  191. Run `rake dojo` to get a Dojo compatible plugin file in the `mustache-dojo/`
  192. directory.
  193. Run `rake yui` to get a Yui compatible plugin file in the `mustache-yui/`
  194. directory.
  195. Run `rake commonjs` to get a CommonJS compatible plugin file in the
  196. `mustache-commonjs/` directory which you can also use with [Node.js][].
  197. ## Testing
  198. To run the mustache.js test suite, run `rake spec`. All specs will be run first with JavaScriptCore (using `jsc`)
  199. and again with Rhino, using `java org.mozilla.javascript.tools.shell.Main`.
  200. JavaScriptCore is used from the OSX default location:
  201. /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
  202. To install Rhino on OSX, follow [these instructions](Rhino Install).
  203. ### Adding Tests
  204. Tests are located in the `examples/` directory. Adding a new test requires three files. Here's an example to add a test named "foo":
  205. `examples/foo.html` (the template):
  206. foo {{bar}}
  207. `examples/foo.js` (the view context):
  208. var foo = {
  209. bar: "baz"
  210. };
  211. `examples/foo.txt` (the expected output):
  212. foo baz
  213. [jQuery]: http://jquery.com/
  214. [Dojo]: http://www.dojotoolkit.org/
  215. [Yui]: http://developer.yahoo.com/yui/
  216. [CommonJS]: http://www.commonjs.org/
  217. [Node.js]: http://nodejs.org/
  218. [Rhino Install]: http://michaux.ca/articles/installing-rhino-on-os-x