1.0.0 • Published 5 years ago

@seregpie/render-template v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

renderTemplate

renderTemplate(template, directive, {
	regex = /{{\s*([^\s{}]+)\s*}}/g,
})

Renders a string template with the given directive.

argumentdescription
templateA string to render.
directiveAn object or an array to be used to replace the matches.
regexA regular expression.

Returns the rendered string.


Access and change the default configuration.

let {
	regex,
} = renderTemplate;

setup

npm

npm i @seregpie/render-template

ES module

import renderTemplate from '@seregpie/render-template';

browser

<script src="https://unpkg.com/@seregpie/render-template"></script>

usage

let string = renderTemplate('Hello, {{ name }}. You have {{ unreadMessagesCount }} unread messages.', {
	name: 'Alice',
	unreadMessagesCount() {
		return 1 + 1;
	},
});
// => 'Hello, Alice. You have 2 unread messages.'

let string = renderTemplate('My top three favorite fruits are {{ 0 }}, {{ 1 }} and {{ 2 }}.', [
	'apples',
	'bananas',
	'oranges',
]);
// => 'My top three favorite fruits are apples, bananas and oranges.'