Skip to content

Commit

Permalink
adding example for templating JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Oct 16, 2011
1 parent 2b3ced0 commit 8dca743
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>",
Expand All @@ -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}})}
Expand Down

0 comments on commit 8dca743

Please sign in to comment.