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

https://github.com/nash403/interpolate-tmpl

Simple string interpolation.
https://github.com/nash403/interpolate-tmpl

Last synced: 3 months ago
JSON representation

Simple string interpolation.

Awesome Lists containing this project

README

          

# interpolate-tmpl
#### Simple string interpolation.

Simple interpolation utility based on the JavaScript `RegExp`, `String#replace` and the npm module [`get-safe`](https://www.npmjs.com/package/get-safe "npm module get-safe") for safe access to nested properties.
***
#### Install:
`npm install interpolate-tmpl --save`

#### How to use
```JavaScript

let interpolate = require('interpolate-tmpl');

let str = `


{{ title('Albert',Einstein ) }}


Born on {{birthDate(March, 14, '1879')}}


Died in {{'death.location' }}, {{ death.date }}


`;

let data = {
color:"red",
death: {
location: "Princeton",
date: "April 18, 1955"
},
title(firstname, lastname) {
return `Mr. ${firstname} ${lastname}`;
},
birthDate(month, day, year) {
return `${month} ${day}, ${year}`;
}
};

console.log(interpolate(str, data, "{{}}") ); // Logs interpolated string
```

You can call functions with simple arguments (strings & integers only, no variable) and change the delimiter. Just pass the delimiter as a string to the interpolate function as the last parameter (default is `{{}}`).

**Supported delimiters are:** `{{}}`, `{}`, `[[]]`, `[]`, `<$$>`, `<##>`, `<%%>`.

***
The browser version adds a `interpolate` function to the window object.