# chalk-template

> Terminal string styling with tagged template literals

Latest version **1.1.2** (published 2025-09-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install chalk-template
pnpm add chalk-template
yarn add chalk-template
bun add chalk-template
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2025-09-08 |
| First published | 2017-01-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=14.16 |
| Dependencies | 1 |
| Unpacked size | 13.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 98 |
| Maintainers | sindresorhus |
| Keywords | chalk, template, templates, templating, ansi, styles, color, colour, colors, terminal, console, string, tty, escape, formatting, rgb, 256, shell, xterm, log, logging, command-line, text |

## Links

- npm: https://www.npmjs.com/package/chalk-template
- Repository: https://github.com/chalk/chalk-template
- Homepage: https://github.com/chalk/chalk-template#readme
- Issues: https://github.com/chalk/chalk-template/issues
- Funding: https://github.com/chalk/chalk-template?sponsor=1
- npm.io page: https://npm.io/package/chalk-template

## Dependencies (1)

- [chalk](https://npm.io/package/chalk.md) ^5.2.0

## Alternatives

- [postcss-color-hex-alpha](https://npm.io/package/postcss-color-hex-alpha.md) — 6.4M weekly downloads
- [randomcolor](https://npm.io/package/randomcolor.md) — 348.0K weekly downloads
- [bows](https://npm.io/package/bows.md) — 1.3K weekly downloads
- [ep_prefer_color_scheme](https://npm.io/package/ep_prefer_color_scheme.md) — 260 weekly downloads
- [coc-yank](https://npm.io/package/coc-yank.md) — 61 weekly downloads

## Recent versions

- 1.1.2 (latest) — 2025-09-08
- 1.1.0 — 2023-05-23
- 1.0.0 — 2023-03-18
- 0.5.0 — 2022-12-26
- 0.4.0 — 2022-03-04
- 0.3.1 — 2022-01-18
- 0.3.0 — 2022-01-17
- 0.2.0 — 2021-11-04
- 0.1.0 — 2017-01-19

## README

# chalk-template

> Terminal string styling with [tagged template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates)

## Install

```sh
npm install chalk-template
```

## Usage

For printing to standard output (stdout):

```js
import chalkTemplate from 'chalk-template';
import chalk from 'chalk';

console.log(chalkTemplate`
CPU: {red ${cpu.totalPercent}%}
RAM: {green ${ram.used / ram.total * 100}%}
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
`);

console.log(chalk.red.bgBlack(chalkTemplate`2 + 3 = {bold ${2 + 3}}`));

const miles = 18;
const calculateFeet = miles => miles * 5280;

console.log(chalkTemplate`
	There are {bold 5280 feet} in a mile.
	In {bold ${miles} miles}, there are {green.bold ${calculateFeet(miles)} feet}.
`);

console.log(chalkTemplate`
	There are also {#FF0000 shorthand hex styles} for
	both the {#ABCDEF foreground}, {#:123456 background},
	or {#ABCDEF:123456 both}.
`);
```

For printing to standard error (stderr):

```js
import {chalkTemplateStderr} from 'chalk-template';

console.error(chalkTemplateStderr`
CPU: {red ${cpu.totalPercent}%}
RAM: {green ${ram.used / ram.total * 100}%}
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
`);
```

## API

Blocks are delimited by an opening curly brace (`{`), a style, some content, and a closing curly brace (`}`).

Template styles are chained exactly like normal [Chalk](https://github.com/chalk/chalk) styles. The following two statements are equivalent:

```js
import chalk from 'chalk';
import chalkTemplate from 'chalk-template';

console.log(chalk.bold.rgb(10, 100, 200)('Hello!'));
console.log(chalkTemplate`{bold.rgb(10,100,200) Hello!}`);
```

Note that function styles (`rgb()`, etc.) may not contain spaces between parameters.

All interpolated values (`` chalkTemplate`${foo}` ``) are converted to strings via the `.toString()` method. All curly braces (`{` and `}`) in interpolated value strings are escaped.

## Template function

This function can be useful if you need to wrap the template function. However, prefer the default export whenever possible.

**Note:** It's up to you to properly escape the input.

```js
import {template} from 'chalk-template';

console.log(template('Today is {red hot}'));
```

```js
import {templateStderr} from 'chalk-template';

console.error(templateStderr('Today is {red hot}'));
```

## Create template functions using a custom Chalk instance

The `makeTemplate` and `makeTaggedTemplate` functions are exported so functions can be created using a custom Chalk instance.

**Note:** When using a function created with `makeTemplate`, it's up to you to properly escape the input.

```js
import {Chalk} from 'chalk'
import {makeTemplate, makeTaggedTemplate} from 'chalk-template';

const template = makeTemplate(new Chalk({level: 3}));
const chalkTemplate = makeTaggedTemplate(new Chalk({level: 3}));

console.log(template('Today is {red hot}'));
console.log(chalkTemplate`Today is {red hot}`);
```

## Related

- [chalk](https://github.com/chalk/chalk) - Terminal string styling done right
- [chalk-cli](https://github.com/chalk/chalk-cli) - Style text from the terminal

## Maintainers

- [Sindre Sorhus](https://github.com/sindresorhus)
- [Josh Junon](https://github.com/qix-)

---
_Source: https://npm.io/package/chalk-template · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
