1.1.0 • Published 9 months ago
go-template-fn v1.1.0
Go Template
String template and interpolation utility
- version: 1.1.0
- license: GNU LGPLv3
Installation
npm i go-template-fnor
yarn add go-template-fnUsage
ES6
import template from 'go-template-fn'
const greet = template("Hello, ${name}");
greet({name: "John"}); // => Hello, JohnNode
const template = require('go-template-fn');
const greet = template("Hello, ${name}");
greet({name: "John"}); // => Hello, JohnWeb browser
<script src="dist/go-template-fn.min.js"></script>
<script>
const greet = template("Hello, ${name}");
greet({name: "John"}); // => Hello, John
</script>Documentation
Table of Contents
template
Creates a compiled template function that can interpolate values into placeholders in the template.
Parameters
strTemplatestring The string template to compile.optionsObject? The compilation options.options.startTagstring The start tag for a placeholder. (optional, default"${")options.endTagstring The end tag for a placeholder. (optional, default"}")options.returnAsFragmentsboolean Iftrue, returns the output as an array of fragments; iffalse, joins the fragments and returns as a concatenated string. (optional, defaultfalse)
Examples
const greet = template("Hello, ${name}");
greet({ name: "John" }); // => Hello, JohnCustom tags
const greet = template("Hello, <%name%>", {startTag: "<%", endTag: "%>"});
greet({ name: "John" }); // => Hello, JohnReturning the result as a fragments array
const greet = template("Hello, ${name}! You've turned ${age} today.", { returnAsFragments: true });
greet({ name: "John", age: 20 }); // => ["Hello, ", "John", "! You've turned ", 20, " today."]Returns Function The compiled template function.
Meta
- since: 1.0.0