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