Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/toddmotto/interpolate
Micro templating engine, maps Object property values to a String template
https://github.com/toddmotto/interpolate
Last synced: 12 days ago
JSON representation
Micro templating engine, maps Object property values to a String template
- Host: GitHub
- URL: https://github.com/toddmotto/interpolate
- Owner: toddmotto
- License: other
- Created: 2014-07-07T21:22:29.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-07-23T15:40:26.000Z (over 10 years ago)
- Last Synced: 2024-10-12T08:10:10.754Z (28 days ago)
- Language: JavaScript
- Homepage:
- Size: 298 KB
- Stars: 56
- Watchers: 4
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# interpolate.js [![Build Status](https://travis-ci.org/toddmotto/interpolate.svg)](https://travis-ci.org/toddmotto/interpolate)
Micro templating engine module weighing <1KB, maps `Object` property values to a handlebar templated `String`. Returns a `closure` which calls a unique `Object` as single argument against the cached template. Doesn't compile to DOM nodes, merely interpolates to `String`, if you want to compile to live DOM [use this additional function](http://jsfiddle.net/toddmotto/2QZz4).
> [Live demo](http://jsfiddle.net/toddmotto/F4k2F)
```javascript
var template = [
'
'{{ name }}',
'{{ age }}',
'
].join('');
var data = {
name: 'Todd Motto',
age: 23,
location: 'United Kingdom'
};
//
interpolate(template)(data);
```
The initial `interpolate()` call caches the template internally, further calls will reference this template whilst mapping Object values:
```javascript
var template = [
'
'{{ name }}',
'{{ age }}',
'
].join('');
var data = [{
name: 'Todd Motto',
age: 23,
location: 'United Kingdom'
},{
name: 'Travis Barker',
age: 38,
location: 'United States'
}];
var render = interpolate(template);
for (var i = 0; i < data.length; i++) {
// iterated Objects called against same template
var done = render(data[i]);
// 0:
// 1:
console.log(done);
}
```
Support for deep Object properties is also there:
```javascript
var template = [
'',
'{{ favourite.language }}',
''
].join('');
var data = {
favourite: {
language: 'JavaScript'
}
}
var render = interpolate(template);
console.log(render(data)); // JavaScript
```
## Installing with Bower
```
bower install https://github.com/toddmotto/interpolate.git
```
## Manual installation
Ensure you're using the files from the `dist` directory (contains compiled production-ready code). Ensure you place the script before the closing `
// interpolate module available