You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
пре 16 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. ### View Partials
  56. mustache.js supports a quite powerful but yet simple view partial mechanism. Use the
  57. following syntax for partials: `{{>partial_name}}`
  58. var view = {
  59. name: "Joe",
  60. winnings: {
  61. value: 1000,
  62. taxed_value: function() {
  63. return this.value - (this.value * 0.4);
  64. }
  65. }
  66. };
  67. var template = "Welcome, {{name}}! {{>winnings}}"
  68. var partials = {
  69. winnings: "You just won ${{value}} (which is ${{taxed_value}} after tax)"};
  70. var output = Mustache.to_html(template, view, partials)
  71. output will be:
  72. Welcome, Joe! You just won $1000 (which is $600 after tax)
  73. You invoke a partial with `{{>winnings}}`. Invoking the partial `winnings` will tell
  74. mustache.js to look for a object in the context's property `winnings`. It will then
  75. use that object as the context for the template found in `partials` for `winnings`.
  76. ## Escaping
  77. mustache.js does escape all values when using the standard double mustache syntax.
  78. Characters which will be escaped: `& \ " < >`. To disable escaping, simply use
  79. tripple mustaches like `{{{unescaped_variable}}}`.
  80. 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`.
  81. ## Streaming
  82. To stream template results out of mustache.js, you can pass an optional `send()`
  83. callback to the `to_html()` call:
  84. Mustache.to_html(template, view, partials, function(line) {
  85. print(line);
  86. });
  87. ## Pragmas
  88. Pragma tags let you alter the behaviour of mustache.js. They have the format of
  89. {{%PRAGMANAME}}
  90. and they accept options:
  91. {{%PRAGMANAME option=value}}
  92. ### IMPLICIT-ITERATOR
  93. When using a block to iterate over an enumerable (Array), mustache.js expects an
  94. objects as enumerable items. The implicit iterator pragma enables optional behaviour
  95. of allowing literals as enumerable items. Consider this view:
  96. var view = {
  97. foo: [1, 2, 3, 4, 5, "french"]
  98. };
  99. The following template can iterate over the member `foo`:
  100. {{%IMPLICIT-ITERATOR}}
  101. {{#foo}}
  102. {{.}}
  103. {{/foo}}
  104. If you don't like the dot in there, the pragma accepts an option to set your own
  105. iteration marker:
  106. {{%IMPLICIT-ITERATOR iterator=bob}}
  107. {{#foo}}
  108. {{bob}}
  109. {{/foo}}
  110. ## More Examples and Documentation
  111. See `examples/` for more goodies and read the [original mustache docs][m]
  112. ## Command Line
  113. See `mustache(1)` man page or
  114. <http://defunkt.github.com/mustache/mustache.1.html>
  115. for command line docs.
  116. Or just install it as a RubyGem:
  117. $ gem install mustache
  118. $ mustache -h
  119. [m]: http://github.com/defunkt/mustache/#readme
  120. [node.js]: http://nodejs.org
  121. [couchdb]: http://couchdb.apache.org