Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding example for templating JSON

  • Loading branch information...
commit 8dca743c10164f373379d246ef8ae48814f2dbf7 1 parent 2b3ced0
Addy Osmani authored
Showing with 35 additions and 1 deletion.
  1. +35 −1 README.md
36 README.md
View
@@ -74,7 +74,7 @@ console.log(templatez(markup, data));
```
-Example: Templating an array
+Example: Templating an array of objects
```javascript
var markup = "<li><b>{{Name}}</b> ({{ReleaseYear}})</li>",
@@ -100,6 +100,40 @@ while(len--) {
console.log('Test:' + template);
```
+Example: Templating JSON
+
+```javascript
+<script>
+var markup = "<li><b>{{Name}}</b> ({{ReleaseYear}}), Rating:{{Rating}}/5</li>",
+ template = "",
+ len = 0;
+
+var jsonMovies = {
+ "movie1": {
+ "Name": "TheRedViolin",
+ "ReleaseYear": "1998",
+ "Rating": "3"
+
+ },
+ "movie2": {
+ "Name": "EyesWideShut",
+ "ReleaseYear": "1999",
+ "Rating" : "2.5"
+ },
+ "movie3": {
+ "Name": "TheInheritance",
+ "ReleaseYear": "1976",
+ "Rating": "3"
+ }
+};
+
+for(var item in jsonMovies){
+ template += templatez(markup, jsonMovies[item]);
+}
+console.log('Test:' + template);
+</script>
+```
+
#184-byte version
```javascript
function t(a,b){return a.replace(RegExp("{{\\s*([a-z0-9_][\\.a-z0-9_]*)\\s*}}","gi"),function(a,c){var d=c.split("."),e=d.length,f=b,g=0;for(;g<e;g++){f=f[d[g]];if(g===e-1)return f}})}
Please sign in to comment.
Something went wrong with that request. Please try again.