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.
- Host: GitHub
- URL: https://github.com/nash403/interpolate-tmpl
- Owner: nash403
- Created: 2016-09-18T16:17:09.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-09-20T17:24:24.000Z (about 9 years ago)
- Last Synced: 2025-05-05T16:19:13.972Z (5 months ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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
```JavaScriptlet 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.