Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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'
    };

    //

  • Todd Motto23

  • 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:

  • Todd Motto23

  • // 1:
  • Travis Barker38

  • 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