{ "name": "mustache", "version": "0.7.2", "description": "Logic-less {{mustache}} templates with JavaScript", "author": { "name": "mustache.js Authors", "email": "http://github.com/janl/mustache.js" }, "keywords": [ "mustache", "template", "templates", "ejs" ], "main": "./mustache.js", "devDependencies": { "mocha": "1.5.0" }, "volo": { "url": "https://raw.github.com/janl/mustache.js/0.7.2/mustache.js" }, "scripts": { "test": "mocha test" }, "readme": "# mustache.js - Logic-less {{mustache}} templates with JavaScript\n\n> What could be more logical awesome than no logic at all?\n\n[mustache.js](http://github.com/janl/mustache.js) is an implementation of the [mustache](http://mustache.github.com/) template system in JavaScript.\n\n[Mustache](http://mustache.github.com/) is a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object.\n\nWe call it \"logic-less\" because there are no if statements, else clauses, or for loops. Instead there are only tags. Some tags are replaced with a value, some nothing, and others a series of values.\n\nFor a language-agnostic overview of mustache's template syntax, see the `mustache(5)` [manpage](http://mustache.github.com/mustache.5.html).\n\n## Where to use mustache.js?\n\nYou can use mustache.js to render mustache templates anywhere you can use JavaScript. This includes web browsers, server-side environments such as [node](http://nodejs.org/), and [CouchDB](http://couchdb.apache.org/) views.\n\nmustache.js ships with support for both the [CommonJS](http://www.commonjs.org/) module API and the [Asynchronous Module Definition](https://github.com/amdjs/amdjs-api/wiki/AMD) API, or AMD.\n\n## Who uses mustache.js?\n\nAn updated list of mustache.js users is kept [on the Github wiki](http://wiki.github.com/janl/mustache.js/beard-competition). Add yourself or your company if you use mustache.js!\n\n## Usage\n\nBelow is quick example how to use mustache.js:\n\n var view = {\n title: \"Joe\",\n calc: function () {\n return 2 + 4;\n }\n };\n\n var output = Mustache.render(\"{{title}} spends {{calc}}\", view);\n\nIn this example, the `Mustache.render` function takes two parameters: 1) the [mustache](http://mustache.github.com/) template and 2) a `view` object that contains the data and code needed to render the template.\n\n## Templates\n\nA [mustache](http://mustache.github.com/) template is a string that contains any number of mustache tags. Tags are indicated by the double mustaches that surround them. `{{person}}` is a tag, as is `{{#person}}`. In both examples we refer to `person` as the tag's key.\n\nThere are several types of tags available in mustache.js.\n\n### Variables\n\nThe most basic tag type is a simple variable. A `{{name}}` tag renders the value of the `name` key in the current context. If there is no such key, nothing is rendered.\n\nAll variables are HTML-escaped by default. If you want to render unescaped HTML, use the triple mustache: `{{{name}}}`. You can also use `&` to unescape a variable.\n\nView:\n\n {\n \"name\": \"Chris\",\n \"company\": \"GitHub\"\n }\n\nTemplate:\n\n * {{name}}\n * {{age}}\n * {{company}}\n * {{{company}}}\n * {{&company}}\n\nOutput:\n\n * Chris\n *\n * <b>GitHub</b>\n * GitHub\n * GitHub\n\nJavaScript's dot notation may be used to access keys that are properties of objects in a view.\n\nView:\n\n {\n \"name\": {\n \"first\": \"Michael\",\n \"last\": \"Jackson\"\n },\n \"age\": \"RIP\"\n }\n\nTemplate:\n\n * {{name.first}} {{name.last}}\n * {{age}}\n\nOutput:\n\n * Michael Jackson\n * RIP\n\n### Sections\n\nSections render blocks of text one or more times, depending on the value of the key in the current context.\n\nA section begins with a pound and ends with a slash. That is, `{{#person}}` begins a `person` section, while `{{/person}}` ends it. The text between the two tags is referred to as that section's \"block\".\n\nThe behavior of the section is determined by the value of the key.\n\n#### False Values or Empty Lists\n\nIf the `person` key does not exist, or exists and has a value of `null`, `undefined`, or `false`, or is an empty list, the block will not be rendered.\n\nView:\n\n {\n \"person\": false\n }\n\nTemplate:\n\n Shown.\n {{#person}}\n Never shown!\n {{/person}}\n\nOutput:\n\n Shown.\n\n#### Non-Empty Lists\n\nIf the `person` key exists and is not `null`, `undefined`, or `false`, and is not an empty list the block will be rendered one or more times.\n\nWhen the value is a list, the block is rendered once for each item in the list. The context of the block is set to the current item in the list for each iteration. In this way we can loop over collections.\n\nView:\n\n {\n \"stooges\": [\n { \"name\": \"Moe\" },\n { \"name\": \"Larry\" },\n { \"name\": \"Curly\" }\n ]\n }\n\nTemplate:\n\n {{#stooges}}\n {{name}}\n {{/stooges}}\n\nOutput:\n\n Moe\n Larry\n Curly\n\nWhen looping over an array of strings, a `.` can be used to refer to the current item in the list.\n\nView:\n\n {\n \"musketeers\": [\"Athos\", \"Aramis\", \"Porthos\", \"D'Artagnan\"]\n }\n\nTemplate:\n\n {{#musketeers}}\n * {{.}}\n {{/musketeers}}\n\nOutput:\n\n * Athos\n * Aramis\n * Porthos\n * D'Artagnan\n\nIf the value of a section variable is a function, it will be called in the context of the current item in the list on each iteration.\n\nView:\n\n {\n \"beatles\": [\n { \"firstName\": \"John\", \"lastName\": \"Lennon\" },\n { \"firstName\": \"Paul\", \"lastName\": \"McCartney\" },\n { \"firstName\": \"George\", \"lastName\": \"Harrison\" },\n { \"firstName\": \"Ringo\", \"lastName\": \"Starr\" }\n ],\n \"name\": function () {\n return this.firstName + \" \" + this.lastName;\n }\n }\n\nTemplate:\n\n {{#beatles}}\n * {{name}}\n {{/beatles}}\n\nOutput:\n\n * John Lennon\n * Paul McCartney\n * George Harrison\n * Ringo Starr\n\n#### Functions\n\nIf the value of a section key is a function, it is called with the section's literal block of text, un-rendered, as its first argument. The second argument is a special rendering function that uses the current view as its view argument. It is called in the context of the current view object.\n\nView:\n\n {\n \"name\": \"Tater\",\n \"bold\": function () {\n return function (text, render) {\n return \"\" + render(text) + \"\";\n }\n }\n }\n\nTemplate:\n\n {{#bold}}Hi {{name}}.{{/bold}}\n\nOutput:\n\n Hi Tater.\n\n### Inverted Sections\n\nAn inverted section opens with `{{^section}}` instead of `{{#section}}`. The block of an inverted section is rendered only if the value of that section's tag is `null`, `undefined`, `false`, or an empty list.\n\nView:\n\n {\n \"repos\": []\n }\n\nTemplate:\n\n {{#repos}}{{name}}{{/repos}}\n {{^repos}}No repos :({{/repos}}\n\nOutput:\n\n No repos :(\n\n### Comments\n\nComments begin with a bang and are ignored. The following template:\n\n