DEV Community

Cover image for copier template variables
Waylon Walker
Waylon Walker

Posted on β€’ Originally published at waylonwalker.com

3 2

copier template variables

I've been looking for a templating tool for awhile that works well with single files. My go to templating tool cookiecutter does not work for single files, it needs to put files into a directory underneath of it.

template variables

By default copier uses double square brackets for its variables. variables in files, directory_names, or file_names will be substituted for their value once you render them.

# hello-py/hello.py.tmpl
print('hello-[[name]]')
Enter fullscreen mode Exit fullscreen mode

note! by default copier will not inject variables into your
[[template-strings]] unless you use a .tmpl suffix.

Before running copier we need to tell copier what variables to ask for, we do this with a copier.yml file.

# copier.yml
name:
  default: my_name
  type: str
  help: What is your name
Enter fullscreen mode Exit fullscreen mode

installing copier

I prefer to install cli tools that I need globally with pipx, this always gives me access to the tool without worrying about dependency conflicts, bloating my system site-packages, or managing a separate virtual environment for it myself.

pipx install copier
Enter fullscreen mode Exit fullscreen mode

running copier

When running copier copy we pass in the directory of the template, and the directory that we want to render the template into.

copier copy hello-py .
Enter fullscreen mode Exit fullscreen mode

note! the directory '.' is often referred to in cli programs to
represent the current working directory that we are calling the
command from.

results

The resulting files will have your variables injected into them if you have setup your template and copier.yml up correctly.

print('hello-you')
Enter fullscreen mode Exit fullscreen mode

Discuss

Let me know, what templating tool you use. I am really looking to double down on my templates soon and would really like to hear if you have a good workflow.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay