tmplr is a versatile templating tool that lets you quickly create a repository from a template customized for your needs. By running a recipe it can substitute placeholder variables with your own values making the repository truly your own. For example it can update package.json
with dynamic values from git context. It can do much more, put for the purpose of this post we will concentrate on literal values in code files.
Let’s say you have a JavaScript code that performs some AWS SDK/CDK actions, and it requires AWS account IDs – for dev and prod deployment. In a regular repo you would define config.js
something like this:
const awsAccounts = { dev: '1234567890', prod: '0987654321' }
But if we’re building a template repo – we don’t want to hardcode those values. Instead we could use tmplr
placeholder variables:
const awsAccounts = { dev: '{{ tmplr.dev_account_id }}', prod: '{{ tmplr.prod_account_id }}' }
Save this file as config.tmplr.js
– it becomes our template. A user of your template then can create a tmplr
recipe:
steps: - read: dev_account_id eval: '1234567890' - read: prod_account_id eval: '0987654321' - copy: config.tmplr.js to: config.js
then save this it into .tmplr.yml
file into repo root, and run tmplr
cli command – it will create config.js
file from config.tmplr.js
substituting variables for values from the recipe.
It works great, but there is a problem. Continue reading →