# template-literal-file

> Load template literals (template strings) from a file.

Latest version **1.1.0** (published 2026-06-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install template-literal-file
pnpm add template-literal-file
yarn add template-literal-file
bun add template-literal-file
```

## Health

**Score 50/100 (C)** — status: active.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2026-06-12 |
| First published | 2022-10-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Tor Brekke Skjøtskift |
| Maintainers | torbs |
| Keywords | template literals, template strings |

## Links

- npm: https://www.npmjs.com/package/template-literal-file
- Repository: https://github.com/torbs/template-literal-file
- Homepage: https://github.com/torbs/template-literal-file#readme
- Issues: https://github.com/torbs/template-literal-file/issues
- npm.io page: https://npm.io/package/template-literal-file

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2026-06-12
- 1.0.9 — 2023-02-10
- 1.0.8 — 2022-10-25
- 1.0.7 — 2022-10-25
- 1.0.6 — 2022-10-25
- 1.0.5 — 2022-10-25
- 1.0.4 — 2022-10-25
- 1.0.3 — 2022-10-25
- 1.0.2 — 2022-10-25
- 1.0.1 — 2022-10-24

## README

# template-literal-file

Have template literals in their own file and compile them to a function.

## Installation

```
npm install template-literal-file
```

## Usage

Load a template literal file using the `renderFile` function:

```js
import { renderFile } from 'template-literal-file';

const template = renderFile('./test.html');

const html = template({ myVar: 'test' });

console.log(html); // <div>test</div>
```

## Template literal file

```
<div>${myVar}</div>
```

The object keys passed to the returned function become variables inside the template. Any JavaScript expression valid inside a template literal works:

```
<ul>${items.map(i => `<li>${i}</li>`).join('')}</ul>
```

## API

### `renderFile(filePath)`

Reads the file at `filePath` and returns a reusable template function. The file is read once and cached — subsequent calls with the same path return the cached function.

- **Relative paths** (e.g. `./template.html`) resolve against the directory of the file that calls `renderFile`.
- **Root-relative paths** (starting with `/`, e.g. `/templates/page.html`) resolve against `process.cwd()`.

### `templateFn(vars?)`

The function returned by `renderFile`. Accepts an optional plain object; each key becomes a variable name available inside the template.

## Security

Template files are executed via `new Function()`. Treat them as trusted code — do not render template files from untrusted sources.

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