string-template
A simple string template function based on named or indexed arguments
Example
var format = var greeting // Format using an object hash with keys matching [0-9a-zA-Z]+ greeting = // greeting -> "Hello Robert, you have 12 unread messages" // Format using a number indexed array greeting = // greeting -> "Hello Robert, you have 12 unread messages" // Format using optional arguments greeting = // greeting -> "Hello Robert, you have 12 unread messages" // Escape {} pairs by using double {{}} var text = // text -> "{0}"
Compiling templates
string-template
exposes two template compiling options for when you need the
additional performance. Arguments passed to the compiled template are of the
same structure as the main string-template
function, so either a single
object/array or a list of arguments.
var compile = var greetingTemplate = var greeting = // -> "Hello Robert, you have 12 unread messages"
Passing a truthy second argument to compile
will opt into using new Function
to generate a function. The function returned contains a literal string
concatenation statement, interleaving the correct arguments you have passed in.
var compile = var greetingTemplate = // -> greetingTemplate generated using new Function var greeting = // -> "Hello Robert, you have 12 unread messages"
Installation
npm install string-template
Contributors
- Matt-Esch