@seregpie/render-template

1.0.0 • Public • Published

renderTemplate

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

Renders a string template with the given directive.

argument description
template A string to render.
directive An object or an array to be used to replace the matches.
regex A 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.'

Package Sidebar

Install

npm i @seregpie/render-template

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

3.47 kB

Total Files

4

Last publish

Collaborators

  • seregpie