Comparing version 0.8.0 to 0.8.1
@@ -414,7 +414,10 @@ /*! | ||
Writer.prototype.parse = function (template, tags) { | ||
if (!(template in this.cache)) { | ||
this.cache[template] = parseTemplate(template, tags); | ||
var cache = this.cache; | ||
var tokens = cache[template]; | ||
if (tokens == null) { | ||
tokens = cache[template] = parseTemplate(template, tags); | ||
} | ||
return this.cache[template]; | ||
return tokens; | ||
}; | ||
@@ -497,4 +500,4 @@ | ||
if (!partials) continue; | ||
value = this.parse(isFunction(partials) ? partials(token[1]) : partials[token[1]]); | ||
if (value != null) buffer += this.renderTokens(value, context, partials, originalTemplate); | ||
value = isFunction(partials) ? partials(token[1]) : partials[token[1]]; | ||
if (value != null) buffer += this.renderTokens(this.parse(value), context, partials, value); | ||
break; | ||
@@ -519,3 +522,3 @@ case '&': | ||
mustache.name = "mustache.js"; | ||
mustache.version = "0.8.0"; | ||
mustache.version = "0.8.1"; | ||
mustache.tags = [ "{{", "}}" ]; | ||
@@ -522,0 +525,0 @@ |
{ | ||
"name": "mustache", | ||
"version": "0.8.0", | ||
"version": "0.8.1", | ||
"description": "Logic-less {{mustache}} templates with JavaScript", | ||
@@ -5,0 +5,0 @@ "author": "mustache.js Authors <http://github.com/janl/mustache.js>", |
315
README.md
@@ -27,10 +27,12 @@ # mustache.js - Logic-less {{mustache}} templates with JavaScript | ||
var view = { | ||
title: "Joe", | ||
calc: function () { | ||
return 2 + 4; | ||
} | ||
}; | ||
```js | ||
var view = { | ||
title: "Joe", | ||
calc: function () { | ||
return 2 + 4; | ||
} | ||
}; | ||
var output = Mustache.render("{{title}} spends {{calc}}", view); | ||
var output = Mustache.render("{{title}} spends {{calc}}", view); | ||
``` | ||
@@ -53,22 +55,28 @@ In 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. | ||
{ | ||
"name": "Chris", | ||
"company": "<b>GitHub</b>" | ||
} | ||
```json | ||
{ | ||
"name": "Chris", | ||
"company": "<b>GitHub</b>" | ||
} | ||
``` | ||
Template: | ||
* {{name}} | ||
* {{age}} | ||
* {{company}} | ||
* {{{company}}} | ||
* {{&company}} | ||
```html | ||
* {{name}} | ||
* {{age}} | ||
* {{company}} | ||
* {{{company}}} | ||
* {{&company}} | ||
``` | ||
Output: | ||
* Chris | ||
* | ||
* <b>GitHub</b> | ||
* <b>GitHub</b> | ||
* <b>GitHub</b> | ||
```html | ||
* Chris | ||
* | ||
* <b>GitHub</b> | ||
* <b>GitHub</b> | ||
* <b>GitHub</b> | ||
``` | ||
@@ -79,19 +87,25 @@ JavaScript's dot notation may be used to access keys that are properties of objects in a view. | ||
{ | ||
"name": { | ||
"first": "Michael", | ||
"last": "Jackson" | ||
}, | ||
"age": "RIP" | ||
} | ||
```json | ||
{ | ||
"name": { | ||
"first": "Michael", | ||
"last": "Jackson" | ||
}, | ||
"age": "RIP" | ||
} | ||
``` | ||
Template: | ||
* {{name.first}} {{name.last}} | ||
* {{age}} | ||
```html | ||
* {{name.first}} {{name.last}} | ||
* {{age}} | ||
``` | ||
Output: | ||
* Michael Jackson | ||
* RIP | ||
```html | ||
* Michael Jackson | ||
* RIP | ||
``` | ||
@@ -112,16 +126,22 @@ ### Sections | ||
{ | ||
"person": false | ||
} | ||
```json | ||
{ | ||
"person": false | ||
} | ||
``` | ||
Template: | ||
Shown. | ||
{{#person}} | ||
Never shown! | ||
{{/person}} | ||
```html | ||
Shown. | ||
{{#person}} | ||
Never shown! | ||
{{/person}} | ||
``` | ||
Output: | ||
Shown. | ||
```html | ||
Shown. | ||
``` | ||
@@ -136,21 +156,27 @@ #### Non-Empty Lists | ||
{ | ||
"stooges": [ | ||
{ "name": "Moe" }, | ||
{ "name": "Larry" }, | ||
{ "name": "Curly" } | ||
] | ||
} | ||
```json | ||
{ | ||
"stooges": [ | ||
{ "name": "Moe" }, | ||
{ "name": "Larry" }, | ||
{ "name": "Curly" } | ||
] | ||
} | ||
``` | ||
Template: | ||
{{#stooges}} | ||
<b>{{name}}</b> | ||
{{/stooges}} | ||
```html | ||
{{#stooges}} | ||
<b>{{name}}</b> | ||
{{/stooges}} | ||
``` | ||
Output: | ||
<b>Moe</b> | ||
<b>Larry</b> | ||
<b>Curly</b> | ||
```html | ||
<b>Moe</b> | ||
<b>Larry</b> | ||
<b>Curly</b> | ||
``` | ||
@@ -161,18 +187,24 @@ When looping over an array of strings, a `.` can be used to refer to the current item in the list. | ||
{ | ||
"musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"] | ||
} | ||
```json | ||
{ | ||
"musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"] | ||
} | ||
``` | ||
Template: | ||
{{#musketeers}} | ||
* {{.}} | ||
{{/musketeers}} | ||
```html | ||
{{#musketeers}} | ||
* {{.}} | ||
{{/musketeers}} | ||
``` | ||
Output: | ||
* Athos | ||
* Aramis | ||
* Porthos | ||
* D'Artagnan | ||
```html | ||
* Athos | ||
* Aramis | ||
* Porthos | ||
* D'Artagnan | ||
``` | ||
@@ -183,26 +215,32 @@ If 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. | ||
{ | ||
"beatles": [ | ||
{ "firstName": "John", "lastName": "Lennon" }, | ||
{ "firstName": "Paul", "lastName": "McCartney" }, | ||
{ "firstName": "George", "lastName": "Harrison" }, | ||
{ "firstName": "Ringo", "lastName": "Starr" } | ||
], | ||
"name": function () { | ||
return this.firstName + " " + this.lastName; | ||
} | ||
} | ||
```js | ||
{ | ||
"beatles": [ | ||
{ "firstName": "John", "lastName": "Lennon" }, | ||
{ "firstName": "Paul", "lastName": "McCartney" }, | ||
{ "firstName": "George", "lastName": "Harrison" }, | ||
{ "firstName": "Ringo", "lastName": "Starr" } | ||
], | ||
"name": function () { | ||
return this.firstName + " " + this.lastName; | ||
} | ||
} | ||
``` | ||
Template: | ||
{{#beatles}} | ||
* {{name}} | ||
{{/beatles}} | ||
```html | ||
{{#beatles}} | ||
* {{name}} | ||
{{/beatles}} | ||
``` | ||
Output: | ||
* John Lennon | ||
* Paul McCartney | ||
* George Harrison | ||
* Ringo Starr | ||
```html | ||
* John Lennon | ||
* Paul McCartney | ||
* George Harrison | ||
* Ringo Starr | ||
``` | ||
@@ -215,18 +253,24 @@ #### Functions | ||
{ | ||
"name": "Tater", | ||
"bold": function () { | ||
return function (text, render) { | ||
return "<b>" + render(text) + "</b>"; | ||
} | ||
} | ||
```js | ||
{ | ||
"name": "Tater", | ||
"bold": function () { | ||
return function (text, render) { | ||
return "<b>" + render(text) + "</b>"; | ||
} | ||
} | ||
} | ||
``` | ||
Template: | ||
{{#bold}}Hi {{name}}.{{/bold}} | ||
```html | ||
{{#bold}}Hi {{name}}.{{/bold}} | ||
``` | ||
Output: | ||
<b>Hi Tater.</b> | ||
```html | ||
<b>Hi Tater.</b> | ||
``` | ||
@@ -239,14 +283,20 @@ ### Inverted Sections | ||
{ | ||
"repos": [] | ||
} | ||
```json | ||
{ | ||
"repos": [] | ||
} | ||
``` | ||
Template: | ||
{{#repos}}<b>{{name}}</b>{{/repos}} | ||
{{^repos}}No repos :({{/repos}} | ||
```html | ||
{{#repos}}<b>{{name}}</b>{{/repos}} | ||
{{^repos}}No repos :({{/repos}} | ||
``` | ||
Output: | ||
No repos :( | ||
```html | ||
No repos :( | ||
``` | ||
@@ -257,7 +307,11 @@ ### Comments | ||
<h1>Today{{! ignore me }}.</h1> | ||
```html | ||
<h1>Today{{! ignore me }}.</h1> | ||
``` | ||
Will render as follows: | ||
<h1>Today.</h1> | ||
```html | ||
<h1>Today.</h1> | ||
``` | ||
@@ -274,7 +328,11 @@ Comments may contain newlines. | ||
<%= partial :next_more, :start => start, :size => size %> | ||
```html+erb | ||
<%= partial :next_more, :start => start, :size => size %> | ||
``` | ||
Mustache requires only this: | ||
{{> next_more}} | ||
```html | ||
{{> next_more}} | ||
``` | ||
@@ -296,9 +354,17 @@ Why? Because the `next_more.mustache` file will inherit the `size` and `start` variables from the calling context. In this way you may want to think of partials as includes, or template expansion, even though it's not literally true. | ||
<h2>Names</h2> | ||
{{#names}} | ||
<strong>{{name}}</strong> | ||
{{/names}} | ||
```html | ||
<h2>Names</h2> | ||
{{#names}} | ||
<strong>{{name}}</strong> | ||
{{/names}} | ||
``` | ||
In mustache.js an object of partials may be passed as the third argument to `Mustache.render`. The object should be keyed by the name of the partial, and its value should be the partial text. | ||
```js | ||
Mustache.render(template, view, { | ||
user: userTemplate | ||
}); | ||
``` | ||
### Set Delimiter | ||
@@ -310,7 +376,9 @@ | ||
* {{ default_tags }} | ||
{{=<% %>=}} | ||
* <% erb_style_tags %> | ||
<%={{ }}=%> | ||
* {{ default_tags_again }} | ||
``` | ||
* {{ default_tags }} | ||
{{=<% %>=}} | ||
* <% erb_style_tags %> | ||
<%={{ }}=%> | ||
* {{ default_tags_again }} | ||
``` | ||
@@ -323,28 +391,13 @@ Here we have a list with three items. The first item uses the default tag style, the second uses ERB style as defined by the Set Delimiter tag, and the third returns to the default style after yet another Set Delimiter declaration. | ||
### Compiled Templates | ||
## Pre-parsing and Caching Templates | ||
Mustache templates can be compiled into JavaScript functions using `Mustache.compile` for improved rendering performance. | ||
By default, when mustache.js first parses a template it keeps the full parsed token tree in a cache. The next time it sees that same template it skips the parsing step and renders the template much more quickly. If you'd like, you can do this ahead of time using `mustache.parse`. | ||
If you have template views that are rendered multiple times, compiling your template into a JavaScript function will minimise the amount of work required for each re-render. | ||
```js | ||
Mustache.parse(template); | ||
Pre-compiled templates can also be generated server-side, for delivery to the browser as ready to use JavaScript functions, further reducing the amount of client side processing required for initialising templates. | ||
// Then, sometime later. | ||
Mustache.render(template, view); | ||
``` | ||
**Mustache.compile** | ||
Use `Mustache.compile` to compile standard Mustache string templates into reusable Mustache template functions. | ||
var compiledTemplate = Mustache.compile(stringTemplate); | ||
The function returned from `Mustache.compile` can then be called directly, passing in the template data as an argument (with an object of partials as an optional second parameter), to generate the final output. | ||
var templateOutput = compiledTemplate(templateData); | ||
**Mustache.compilePartial** | ||
Template partials can also be compiled using the `Mustache.compilePartial` function. The first parameter of this function, is the name of the partial as it appears within parent templates. | ||
Mustache.compilePartial('partial-name', stringTemplate); | ||
Compiled partials are then available to both `Mustache.render` and `Mustache.compile`. | ||
## Plugins for JavaScript Libraries | ||
@@ -351,0 +404,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
459
0
0
41471
22
484