template-file
🔀 Replace {{ variables }} in all your files
Use variables to replace template strings in any type of file.
✨ Some helpful features:
- If you use a JavaScript file as the
dataFile
argument, whatever object the JS exports is used for replacement. - If the value of one of the keys is a function, the result of that function is used for replacement.
⚠️ NOTE: As of right now, only shallow values can be used in files. We know this sucks, and there’s an open issue for it. However, we believe in releasing early and often.
Usage
template-file <dataFile> <sourceGlob> <destination>
Arguments
- data - Data file in JSON; used to replace variables in source files
- sourceGlob - Files to process; see glob for syntax
- destination - Destination directory where processed files go
Examples
ℹ️ TIP: Remember to place single 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.
Just handle one file:
template-file data.json template.txt build/
Compile all .abc
files in src/
to build/
:
template-file stuff.json 'src/**/*.abc' build/
Compile all HTML files in src/
to dist/
using the result of a JavaScript module:
template-file retrieveData.js 'src/**/*.html' './dist'
API
const { renderString, renderTemplateFile } = require('template-file')
const data = {
company: "GS&F",
adjective: "cool"
}
renderString('{{ company }} is {{ adjective }}.', data)
renderTemplateFile('/path/to/file', data)
.then(renderedString => console.log(renderedString))
Install
With either Yarn or npm installed, run one of the following:
# If using Yarn, add to project:
yarn add template-file
# ...or install globally to use anywhere:
yarn global add template-file
# If using npm, add to project:
npm install --save template-file
# ...or install globally to use anywhere:
npm install --global template-file
License
MIT