https://github.com/jamen/lazy-template
Create template strings that are lazy and reusable.
https://github.com/jamen/lazy-template
Last synced: 11 months ago
JSON representation
Create template strings that are lazy and reusable.
- Host: GitHub
- URL: https://github.com/jamen/lazy-template
- Owner: jamen
- License: mit
- Created: 2016-09-01T03:18:51.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-01-29T02:15:35.000Z (over 9 years ago)
- Last Synced: 2025-07-12T05:39:02.349Z (11 months ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lazy-template [](https://npmjs.org/package/lazy-template) [](https://travis-ci.org/jamen/lazy-template)
> Create template strings that are lazy and reusable.
```js
const lazy = require('lazy-template')
// Create a lazy template string.
const greet = lazy`Hello ${0}. How is ${1}?`
// Use the returned function to compile.
greet([ 'Earth', 'the weather' ])
// => 'Hello Earth. How is the weather?'
greet([ 'Mars', 'Jamen' ])
// => 'Hello Mars. How is Jamen?'
```
This module is built with [`pixie`](https://github.com/jamen/pixie). You can use array indexes or object keys for the points. Or just provide a different `compile` function all together.
## Installation
```sh
$ npm install --save lazy-template
```
## Usage
### `lazy`
Create a lazy template string. Returns a `compile` function, which when called returns the concatenated string.
```javascript
// Using array indexes:
const compile = lazy`Hello ${0}. How are ${1}?`
// Using object keys:
const compile = lazy`Hello ${'world'}. How is ${'thing'}?`
```
### `compile(data, [compile])`
Compile the "lazy string" using some data. Returns resulting string.
- data (`Array`|`Object`): Data to use in your template.
- compile (`Function`): A custom function to replace [`pixie.compile`](https://github.com/jamen/pixie#pixie_compile)
```js
const compile = lazy`Hello ${0}.`
compile([ 'Earth' ])
// => 'Hello Earth.'
const compile = lazy`Hello ${'world'}.`
compile({ world: 'Mars' });
// => 'Hello Mars.'
const compile = lazy`Hello ${0}. How is ${1}?`
compile([ 'Pluto', 'Jamen' ], function (template, data) {
// Compile `template` and `data`
// Return result
})
```
## License
MIT © [Jamen Marz](https://github.com/jamen)