Skip to main content
This release is 5 versions behind 1.33.0 — the latest version of @es-toolkit/es-toolkit. Jump to latest

@es-toolkit/es-toolkit@1.29.0-dev.958+1d1d0ee6
Built and signed on GitHub Actions

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
3 months ago (1.29.0-dev.958+1d1d0ee6)
function template
template(
string: string,
options?: TemplateOptions,
guard?: unknown,
): ((data?: object) => string) & { source: string; }

Compiles a template string into a function that can interpolate data properties.

This function allows you to create a template with custom delimiters for escaping, evaluating, and interpolating values. It can also handle custom variable names and imported functions.

Examples

Example 1

// Use the "escape" delimiter to escape data properties. const compiled = template('<%- value %>'); compiled({ value: '

' }); // returns '<div>'

Example 2

// Use the "interpolate" delimiter to interpolate data properties. const compiled = template('<%= value %>'); compiled({ value: 'Hello, World!' }); // returns 'Hello, World!'

Example 3

// Use the "evaluate" delimiter to evaluate JavaScript code. const compiled = template('<% if (value) { %>Yes<% } else { %>No<% } %>'); compiled({ value: true }); // returns 'Yes'

Example 4

// Use the "variable" option to specify the data object variable name. const compiled = template('<%= data.value %>', { variable: 'data' }); compiled({ value: 'Hello, World!' }); // returns 'Hello, World!'

Example 5

// Use the "imports" option to import functions. const compiled = template('<%= _.toUpper(value) %>', { imports: { _: { toUpper } } }); compiled({ value: 'hello, world!' }); // returns 'HELLO, WORLD!'

Example 6

// Use the custom "escape" delimiter. const compiled = template('<@ value @>', { escape: /<@([\s\S]+?)@>/g }); compiled({ value: '

' }); // returns '<div>'

Example 7

// Use the custom "evaluate" delimiter. const compiled = template('<# if (value) { #>Yes<# } else { #>No<# } #>', { evaluate: /<#([\s\S]+?)#>/g }); compiled({ value: true }); // returns 'Yes'

Example 8

// Use the custom "interpolate" delimiter. const compiled = template('<$ value $>', { interpolate: /<$([\s\S]+?)$>/g }); compiled({ value: 'Hello, World!' }); // returns 'Hello, World!'

Example 9

// Use the "sourceURL" option to specify the source URL of the template. const compiled = template('hello <%= user %>!', { sourceURL: 'template.js' });

Parameters

string: string
  • The template string.
optional
options: TemplateOptions
  • The options object.
optional
guard: unknown
  • The guard to detect if the function is called with options.

Return Type

((data?: object) => string) & { source: string; }

Returns the compiled template function.

Add Package

deno add jsr:@es-toolkit/es-toolkit

Import symbol

import { template } from "@es-toolkit/es-toolkit/compat";

---- OR ----

Import directly with a jsr specifier

import { template } from "jsr:@es-toolkit/es-toolkit/compat";

Add Package

npx jsr add @es-toolkit/es-toolkit

Import symbol

import { template } from "@es-toolkit/es-toolkit/compat";

Add Package

yarn dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { template } from "@es-toolkit/es-toolkit/compat";

Add Package

pnpm dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { template } from "@es-toolkit/es-toolkit/compat";

Add Package

bunx jsr add @es-toolkit/es-toolkit

Import symbol

import { template } from "@es-toolkit/es-toolkit/compat";