• Stars
    star
    276
  • Rank 149,319 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 11 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A lightweight string replace engine for text-based templates

string-template

build status dependency status coverage report stability index

npm stats

browser support

A simple string template function based on named or indexed arguments

Example

var format = require("string-template")
var greeting

// Format using an object hash with keys matching [0-9a-zA-Z]+

greeting = format("Hello {name}, you have {count} unread messages", {
    name: "Robert",
    count: 12
})
// greeting -> "Hello Robert, you have 12 unread messages"


// Format using a number indexed array

greeting = format("Hello {0}, you have {1} unread messages", ["Robert", 12])
// greeting -> "Hello Robert, you have 12 unread messages"


// Format using optional arguments

greeting = format("Hello {0}, you have {1} unread messages",
     "Robert",
     12)
// greeting -> "Hello Robert, you have 12 unread messages"


// Escape {} pairs by using double {{}}

var text = format("{{0}}")
// 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 = require("string-template/compile")

var greetingTemplate = compile("Hello {0}, you have {1} unread messages")

var greeting = greetingTemplate("Robert", 12)
// -> "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 = require("string-template/compile")

var greetingTemplate = compile("Hello {0}, you have {1} unread messages", true)
// -> greetingTemplate generated using new Function

var greeting = greetingTemplate(["Robert", 12])
// -> "Hello Robert, you have 12 unread messages"

Installation

npm install string-template

Contributors

  • Matt-Esch

MIT Licenced

More Repositories

1

virtual-dom

A Virtual DOM and diffing algorithm
JavaScript
11,667
star
2

vtree

A realtime tree diffing algorithm
JavaScript
128
star
3

vdom

A DOM create and patch algorithm for vtree
JavaScript
88
star
4

http-hash

HTTP router based on a strict path tree structure
JavaScript
76
star
5

http-hash-router

Server route handler for http-hash
JavaScript
34
star
6

underbar

Modular underscore - require("underbar/function")
JavaScript
21
star
7

anaconda

Anaconda is an open, cross-platform runtime for Anaconda for Multimedia Fusion 2, with support for Windows, Linux and Mac OS X.
Python
14
star
8

browser-fft

A cheap FFT by abusing the web audio API
JavaScript
12
star
9

virtual-form

Form interface for virtual-dom
JavaScript
11
star
10

x-is-array

JavaScript
9
star
11

biglist

Efficient rendering for large lists
JavaScript
9
star
12

css3d

Building 3D models using CSS 3D transforms on triangles
JavaScript
6
star
13

ptrack

A small collection of pitch tracking algorithms in C
6
star
14

blender-compiler

A high performance blend function compiler
JavaScript
5
star
15

eventsource-reconnect

JavaScript
4
star
16

isoblend

PDF Blend modes as defined by the Adobe® Supplement to the ISO 32000/-1
JavaScript
4
star
17

radial-camera

Computes the model view matrix for a camera rotated around a point
JavaScript
4
star
18

http-hash-server

HTTP server for http-hash router
JavaScript
4
star
19

c2-mode7

Mode7 pixel shader for Construct 2
FLUX
3
star
20

triangle-homography

JavaScript
3
star
21

triangle-buffer

JavaScript
3
star
22

jsonml-document

JavaScript
3
star
23

event-source-server

Example server showing how to notify event-source-reconnect with keepalive messages
JavaScript
3
star
24

x-is-string

JavaScript
3
star
25

jsonml-walk

JavaScript
3
star
26

simpleCanvas.js

An efficient JavaScript library for rendering rectangles, images and text.
JavaScript
3
star
27

opentty

Open a TTY read stream
JavaScript
3
star
28

c-playground

Playing around with clean and maintainable C styles
C
3
star
29

firstKey

JavaScript
2
star
30

canvas-blend

Blend overlapping images and draw the result
JavaScript
2
star
31

safe64

URI safe base64 encoding
JavaScript
2
star
32

text-assembler

Splits a string into multiple parts, identified by a guid.
JavaScript
2
star
33

arrow-keys

Arrow keys for all!
JavaScript
2
star
34

cssViewport

JavaScript
2
star
35

json-lookup

JavaScript
2
star
36

peer-module

JavaScript
2
star
37

node-tchannel-parser

TChannel frame parser for node.js
C++
1
star
38

unigl

Experimental portable GLES 2.0
C
1
star
39

sitemapper

Parallel web crawler for producing site maps
Go
1
star
40

choke

Rate limit a function call, and cancel waiting calls
JavaScript
1
star
41

strat

RTS engine - just for fun
C
1
star
42

x-is-object

JavaScript
1
star
43

xtendr

Extend with respect for undefined
JavaScript
1
star
44

isoblend-testbed

Compare isoblend implementation with output from Adobe® Photoshop®
JavaScript
1
star