{"id":13626549,"url":"https://github.com/gsandf/template-file","last_synced_at":"2025-08-21T12:30:46.211Z","repository":{"id":19470043,"uuid":"85197105","full_name":"gsandf/template-file","owner":"gsandf","description":"🔀 Replace {{ variables }} in all your files","archived":false,"fork":false,"pushed_at":"2023-10-11T13:50:42.000Z","size":879,"stargazers_count":37,"open_issues_count":7,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-11T12:25:00.136Z","etag":null,"topics":["configuration","template","template-string","tools","variables"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gsandf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-03-16T13:08:42.000Z","updated_at":"2024-11-14T16:55:42.000Z","dependencies_parsed_at":"2023-10-11T04:45:43.861Z","dependency_job_id":"23511a48-ee30-4ea4-bcc4-1012016e7334","html_url":"https://github.com/gsandf/template-file","commit_stats":{"total_commits":86,"total_committers":8,"mean_commits":10.75,"dds":"0.40697674418604646","last_synced_commit":"cac6bad1484373e0c20d9e58f3f067bff14d5632"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsandf%2Ftemplate-file","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsandf%2Ftemplate-file/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsandf%2Ftemplate-file/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsandf%2Ftemplate-file/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gsandf","download_url":"https://codeload.github.com/gsandf/template-file/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229901648,"owners_count":18141739,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["configuration","template","template-string","tools","variables"],"created_at":"2024-08-01T21:02:23.470Z","updated_at":"2024-12-19T23:14:53.578Z","avatar_url":"https://github.com/gsandf.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","tools"],"sub_categories":[],"readme":"# template-file\n\n\u003e 🔀 Replace {{ variables }} in all your files\n\nUse variables to replace template strings in any type of file. This is both a runnable command-line application and JavaScript/TypeScript module.\n\n**✨ Some helpful features:**\n\n- If you use a JavaScript file as the `dataFile` argument, whatever object the JS exports is used for replacement.\n- If the value of one of the keys is a function, the result of that function is used for replacement.\n- Deeply-nested keys can be used for replacements.\n\n## Usage\n\n```shell\ntemplate-file \u003cdataFile\u003e \u003csourceGlob\u003e \u003cdestination\u003e\n```\n\n### Arguments\n\n- **data** - Data file in JSON; used to replace variables in source files\n- **sourceGlob** - Files to process; see [glob](https://npmjs.com/glob) for syntax\n- **destination** - Destination directory where processed files go\n\n**ℹ️ TIP:** Remember to place quotes around your arguments (if they contain asterisks, question marks, etc.) to keep your shell from expanding globs before `template-file` gets to consume them.\n\n### Examples\n\nJust handle one file:\n\n```shell\ntemplate-file data.json template.txt build/\n```\n\nCompile all `.abc` files in `src/` to `build/`:\n\n```shell\ntemplate-file stuff.json 'src/**/*.abc' build/\n```\n\nCompile all HTML files in `src/` to `dist/` using the exported result of a JavaScript module:\n\n```shell\ntemplate-file retrieveData.js 'src/**/*.html' './dist'\n```\n\n## Templates\n\nThis uses templates similar to [mustache](https://mustache.github.io/) templates, but there are some differences.\n\nAnything between `{{` and `}}` can be replaced with a value. Spacing doesn't matter.\n\n```js\nconst template = '{{ location.name }} is {{adjective}}.';\nconst data = {\n  location: { name: 'Nashville' },\n  adjective: 'cool'\n};\n\nrender(template, data); //» 'Nashville is cool.'\n```\n\nTo render a list of items, you can use `{{#example}}` and `{{/example}}`. Empty lists and falsy values aren't rendered:\n\n```js\nconst template = `\n  \u003ch3\u003eFriend List:\u003c/h3\u003e\n  \u003cul\u003e\n    {{#friends}}\n    \u003cli\u003e{{name}}\u003c/li\u003e\n    {{/friends}}\n  \u003c/ul\u003e\n`;\n\nconst data = {\n  friends: [{ name: 'Amanda' }, { name: 'Bryson' }, { name: 'Josh' }]\n};\n\nrender(template, data);\n// \u003ch3\u003eFriend List:\u003c/h3\u003e\n// \u003cul\u003e\n//   \u003cli\u003eAmanda\u003c/li\u003e\n//   \u003cli\u003eBryson\u003c/li\u003e\n//   \u003cli\u003eJosh\u003c/li\u003e\n// \u003c/ul\u003e\n```\n\nIf you have an array of primitive values instead of objects, you can use `{{ this }}` to refer to the current value:\n\n```js\nconst template = `\n### Foods I Like\n\n{{#foods}}\n  - {{ this }}\n{{/foods}}\n`;\n\nconst data = {\n  foods: ['steak', 'eggs', 'avocado']\n};\n\nrender(template, data);\n// ### Foods I Like\n//\n// - steak\n// - eggs\n// - avocado\n```\n\nIf a replacement is a function, it is called with no arguments:\n\n```js\nconst template = `Hello, {{name}}`;\n\nconst data = {\n  name: () =\u003e 'Charles'\n};\n\nrender(template, data); //» Hello, Charles\n```\n\n## API\n\nIn addition to the CLI, this module exports several helpers to programmatically render templates.\n\n**Example:**\n\n```js\nimport { render, renderFile } from 'template-file';\n\nconst data = {\n  location: { name: 'Nashville' },\n  adjective: 'cool'\n};\n\n// Replace variables in string\nrender('{{ location.name }} is {{ adjective }}.', data); //» 'Nashville is cool.'\n\n// Replace variables in a file (same as above, but from a file)\nconst string = await renderFile('/path/to/file', data);\nconsole.log(renderedString);\n```\n\n### `render`\n\n**Type:**\n\n```ts\nfunction render(template: string, data: Data): string;\n```\n\nReplaces values from `data` and returns the rendered string.\n\n```ts\nimport { render } from 'template-file';\n\nconst data = {\n  location: { name: 'Nashville' },\n  adjective: 'cool'\n};\n\nrender('{{ location.name }} is {{ adjective }}.', data); //» 'Nashville is cool.'\n```\n\n### `renderFile`\n\n**Type:**\n\n```ts\nfunction renderFile(filepath: string, data: Data): Promise\u003cstring\u003e;\n```\n\nReads a file replaces values from `data`, and returns the rendered string.\n\n```ts\nimport { renderFile } from 'template-file';\n\n// example.html:\n// \u003ch1\u003eWelcome back, {{ sites.github.username }}!\u003c/h1\u003e\n\nconst data = {\n  name: 'Blake',\n  sites: {\n    github: {\n      username: 'blakek'\n    }\n  }\n};\n\nrenderFile('./example.html', data); //» '\u003ch1\u003eWelcome back, blakek!\u003c/h1\u003e'\n```\n\n### `renderGlob`\n\n**Type:** (note, this may change in a future major version release)\n\n```ts\nfunction renderGlob(\n  sourceGlob: string,\n  data: Data,\n  onFileCallback: (filename: string, contents: string) =\u003e void\n): Promise\u003cvoid\u003e;\n```\n\nFinds files matching a glob pattern, reads those files, replaces values from `data`, and calls a function for each file. Note, no string is returned from the function; values are handled through callbacks for each file.\n\n```ts\nimport { renderGlob } from 'template-file';\n\n// ./templates/profile.html:\n// \u003ch1\u003eWelcome back, {{ name }}!\u003c/h1\u003e\n\n// ./templates/sign-in.html:\n// \u003cp\u003eCurrently signed in as \u003cem\u003e{{ sites.github.username }}\u003cem\u003e.\u003c/p\u003e\n\nconst data = {\n  name: 'Blake',\n  sites: {\n    github: {\n      username: 'blakek'\n    }\n  }\n};\n\nconst files = [];\n\nrenderGlob('./templates/*.html', data, (filename, contents) =\u003e {\n  files.push({ filename, contents });\n});\n\nconsole.log(files);\n// [\n//   {\n//     contents: '\u003ch1\u003eWelcome back, Blake!\u003c/h1\u003e',\n//     filename: './templates/profile.html'\n//   },\n//   {\n//     contents: '\u003cp\u003eCurrently signed in as \u003cem\u003eblakek\u003cem\u003e.\u003c/p\u003e',\n//     filename: './templates/sign-in.html'\n//   }\n// ]\n```\n\n### `renderToFolder`\n\n**Type:**\n\n```ts\nfunction renderToFolder(\n  sourceGlob: string,\n  destination: string,\n  data: Data\n): Promise\u003cvoid\u003e;\n```\n\n```ts\nimport { renderToFolder } from 'template-file';\n\nconst data = {\n  name: 'Blake',\n  sites: {\n    github: {\n      username: 'blakek'\n    }\n  }\n};\n\nrenderToFolder('./templates/*.html', './dist/', data);\n```\n\nFinds files matching a glob pattern, reads those files, replaces values from `data`, and writes a file with the same name to `destination`.\n\n### Upgrading from older versions:\n\nVersion 5 renamed some functions to be simpler:\n\n- `renderString` was renamed `render`\n- `renderTemplateFile` was renamed `renderFile`\n- `renderGlob` and `renderToFolder` were in v4 but were undocumented. The API for `renderGlob` may change in the future, depending on usage.\n\nVersions \u003c 4 could not lookup properties with a dot in the name. This should be possible since version 4. For example, this was not possible before v4.0.0:\n\n```ts\nimport { render } from 'template-file';\n\nconst data = { 'with.dot': 'yep' };\n\nrender('Does this work? {{with.dot}}', data);\n```\n\n## Install\n\nWith either [Yarn](https://yarnpkg.com/) or [npm](https://npmjs.org/) installed, run **one** of the following:\n\n| Task                                     | with Yarn                       | with npm                               |\n| ---------------------------------------- | ------------------------------- | -------------------------------------- |\n| Add this to a project                    | `yarn add template-file`        | `npm install --save template-file`     |\n| Install this as a development dependency | `yarn add --dev template-file`  | `npm install --save-dev template-file` |\n| Install this globally                    | `yarn global add template-file` | `npm install --global template-file`   |\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgsandf%2Ftemplate-file","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgsandf%2Ftemplate-file","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgsandf%2Ftemplate-file/lists"}