25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

README.md 6.8KB

16 yıl önce
16 yıl önce
16 yıl önce
16 yıl önce
16 yıl önce
16 yıl önce
16 yıl önce
16 yıl önce
16 yıl önce
16 yıl önce
16 yıl önce
16 yıl önce
16 yıl önce
16 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 render
  7. templates in your browser, or rendering server-side stuff with [node.js][node.js],
  8. 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:
  12. <http://wiki.github.com/janl/mustache.js/beard-competition>
  13. ## Usage
  14. A quick example how to use mustache.js:
  15. var view = {
  16. title: "Joe",
  17. calc: function() {
  18. return 2 + 4;
  19. }
  20. }
  21. var template = "{{title}} spends {{calc}}";
  22. var html = Mustache.to_html(template, view);
  23. `template` is a simple string with mustache tags and `view` is a JavaScript object containing the.
  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
  27. the `mustache(5)` manpage or
  28. <http://mustache.github.com/mustache.5.html>.
  29. ### Simple Tags
  30. Tags are always surrounded by mustaches like this `{{foobar}}`.
  31. var view = {name: "Joe", say_hello: function(){ return "hello" }}
  32. template = "{{say_hello}}, {{name}}"
  33. ### Conditional Sections
  34. Conditional sections begin with `{{#condition}}` and end with `{{/condition}}`. When
  35. `condition` evaluates to true, the section is rendered, otherwise the hole block will
  36. output nothing at all. `condition` may be a function returning true/false or a simple
  37. boolean.
  38. var view = {condition: function() {
  39. // [...your code goes here...]
  40. return true;
  41. }}
  42. {{#condition}}
  43. I will be visible if condition is true
  44. {{/condition}}
  45. ### Enumerable Sections
  46. Enumerable Sections use the same syntax as condition sections do.
  47. `{{#shopping_items}}` and `{{/shopping_items}}`. Actually the view decides how
  48. mustache.js renders the section. If the view returns an array, it will iterator over
  49. the items. Use `{{.}}` to access the current item inside the 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 unrendered
  57. 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 to
  70. 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 uses a
  101. 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. Use the
  113. 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` will tell
  130. mustache.js to look for a object in the context's property `winnings`. It will then
  131. use that object as the context for the template found in `partials` for `winnings`.
  132. ## Escaping
  133. mustache.js does escape all values when using the standard double mustache syntax.
  134. Characters which will be escaped: `& \ " < >`. To disable escaping, simply use
  135. tripple mustaches like `{{{unescaped_variable}}}`.
  136. 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`.
  137. ## Streaming
  138. To stream template results out of mustache.js, you can pass an optional `send()`
  139. callback to the `to_html()` call:
  140. Mustache.to_html(template, view, partials, function(line) {
  141. print(line);
  142. });
  143. ## Pragmas
  144. Pragma tags let you alter the behaviour of mustache.js. They have the format of
  145. {{%PRAGMANAME}}
  146. and they accept options:
  147. {{%PRAGMANAME option=value}}
  148. ### IMPLICIT-ITERATOR
  149. When using a block to iterate over an enumerable (Array), mustache.js expects an
  150. objects as enumerable items. The implicit iterator pragma enables optional behaviour
  151. of allowing literals as enumerable items. Consider this view:
  152. var view = {
  153. foo: [1, 2, 3, 4, 5, "french"]
  154. };
  155. The following template can iterate over the member `foo`:
  156. {{%IMPLICIT-ITERATOR}}
  157. {{#foo}}
  158. {{.}}
  159. {{/foo}}
  160. If you don't like the dot in there, the pragma accepts an option to set your own
  161. iteration marker:
  162. {{%IMPLICIT-ITERATOR iterator=bob}}
  163. {{#foo}}
  164. {{bob}}
  165. {{/foo}}
  166. ## More Examples and Documentation
  167. See `examples/` for more goodies and read the [original mustache docs][m]
  168. ## Command Line
  169. See `mustache(1)` man page or
  170. <http://defunkt.github.com/mustache/mustache.1.html>
  171. for command line docs.
  172. Or just install it as a RubyGem:
  173. $ gem install mustache
  174. $ mustache -h
  175. [m]: http://github.com/defunkt/mustache/#readme
  176. [node.js]: http://nodejs.org
  177. [couchdb]: http://couchdb.apache.org