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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # mustache.js
  2. > What could be more logical awesome than no logic at all?
  3. Shamless port of http://github.com/defunkt/mustache
  4. by Jan Lehnardt <jan@apache.org>.
  5. Thanks @defunkt for the awesome code.
  6. For a list of implementations (other than JavaScript) and editor
  7. plugins, see <http://defunkt.github.com/mustache/>.
  8. ## Where to Use?
  9. You can use mustache.js rendering stuff in various scenarios. E.g. you can render
  10. templates in your browser, or rendering server-side stuff with [node.js][node.js],
  11. use it for rendering stuff in [CouchDB][couchdb]'s views.
  12. ## Who Uses Mustache?
  13. An updated list is kept on the Github wiki. Add yourself, if you use
  14. mustache.js:
  15. <http://wiki.github.com/janl/mustache.js/beard-competition>
  16. ## Usage
  17. A quick example how to use mustache.js:
  18. var view = {
  19. title: "Joe",
  20. calc: function() {
  21. return 2 + 4;
  22. }
  23. }
  24. var template = "{{title}} spends {{calc}}";
  25. var html = Mustache.to_html(template, view);
  26. `template` is a simple string with mustache tags and `view` is a JavaScript object containing the.
  27. ## Template Tag Types
  28. There are several types of tags currently implemented in mustache.js.
  29. For a language-agnostic overview of Mustache's template syntax, see
  30. the `mustache(5)` manpage or
  31. <http://defunkt.github.com/mustache/mustache.5.html>.
  32. ### Simple Tags
  33. Tags are always surrounded by mustaches like this `{{foobar}}`.
  34. var view = {name: "Joe", say_hello: function(){ return "hello" }}
  35. template = "{{say_hello}}, {{name}}"
  36. ### Conditional Sections
  37. Conditional sections begin with `{{#condition}}` and end with `{{/condition}}`. When
  38. `condition` evaluates to true, the section is rendered, otherwise the hole block will
  39. output nothing at all. `condition` may be a function returning true/false or a simple
  40. boolean.
  41. var view = {condition: function() {
  42. // [...your code goes here...]
  43. return true;
  44. }}
  45. {{#condition}}
  46. I will be visible if condition is true
  47. {{/condition}}
  48. ### Enumerable Sections
  49. Enumerable Sections use the same syntax as condition sections do.
  50. `{{#shopping_items}}` and `{{/shopping_items}}`. Actually the view decides how
  51. mustache.js renders the section. If the view returns an array, it will iterator over
  52. the items. Use `{{.}}` to access the current item inside the enumeration section.
  53. var view = {name: "Joe's shopping card",
  54. items: ["bananas", "apples"]}
  55. var template = "{{name}}: <ul> {{#items}}<li>{{.}}</li>{{/items}} </ul>"
  56. Outputs:
  57. Joe's shopping card: <ul><li>bananas</li><li>apples</li></ul>
  58. ### View Partials
  59. mustache.js supports a quite powerful but yet simple view partial mechanism. Use the
  60. following syntax for partials: `{{>partial_name}}`
  61. var view = {
  62. name: "Joe",
  63. winnings: {
  64. value: 1000,
  65. taxed_value: function() {
  66. return this.value - (this.value * 0.4);
  67. }
  68. }
  69. };
  70. var template = "Welcome, {{name}}! {{>winnings}}"
  71. var partials = {
  72. winnings: "You just won ${{value}} (which is ${{taxed_value}} after tax)"};
  73. var output = Mustache.to_html(template, view, partials)
  74. output will be:
  75. Welcome, Joe! You just won $1000 (which is $600 after tax)
  76. You invoke a partial with `{{>winnings}}`. Invoking the partial `winnings` will tell
  77. mustache.js to look for a object in the context's property `winnings`. It will then
  78. use that object as the context for the template found in `partials` for `winnings`.
  79. ## Escaping
  80. mustache.js does escape all values when using the standard double mustache syntax.
  81. Characters which will be escaped: `& \ " < >`. To disable escaping, simply use
  82. tripple mustaches like `{{{unescaped_variable}}}`.
  83. 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`.
  84. ## Streaming
  85. To stream template results out of mustache.js, you can pass an optional `send()`
  86. callback to the `to_html()` call:
  87. Mustache.to_html(template, view, partials, function(line) {
  88. print(line);
  89. });
  90. ## Pragmas
  91. Pragma tags let you alter the behaviour of mustache.js. They have the format of
  92. {{%PRAGMANAME}}
  93. and they accept options:
  94. {{%PRAGMANAME option=value}}
  95. ### IMPLICIT-ITERATOR
  96. When using a block to iterate over an enumerable (Array), mustache.js expects an
  97. objects as enumerable items. The implicit iterator pragma enables optional behaviour
  98. of allowing literals as enumerable items. Consider this view:
  99. var view = {
  100. foo: [1, 2, 3, 4, 5, "french"]
  101. };
  102. The following template can iterate over the member `foo`:
  103. {{%IMPLICIT-ITERATOR}}
  104. {{#foo}}
  105. {{.}}
  106. {{/foo}}
  107. If you don't like the dot in there, the pragma accepts an option to set your own
  108. iteration marker:
  109. {{%IMPLICIT-ITERATOR iterator=bob}}
  110. {{#foo}}
  111. {{bob}}
  112. {{/foo}}
  113. ## More Examples and Documentation
  114. See `examples/` for more goodies and read the [original mustache docs][m]
  115. ## Command Line
  116. See `mustache(1)` man page or
  117. <http://defunkt.github.com/mustache/mustache.1.html>
  118. for command line docs.
  119. Or just install it as a RubyGem:
  120. $ gem install mustache
  121. $ mustache -h
  122. [m]: http://github.com/defunkt/mustache/#readme
  123. [node.js]: http://nodejs.org
  124. [couchdb]: http://couchdb.apache.org