Build up Reusable Claude Code Workflows with Dynamic Prompt Templating using Gomplate

John Lindquist
InstructorJohn Lindquist

Social Share Links

Tweet

Managing complex prompts for command-line AI tools can be clunky. You either write monolithic prompt files or manually copy and paste code and other context into the terminal. This approach isn't scalable or reusable.

This lesson introduces a powerful solution: gomplate. It's a flexible command-line tool for rendering templates, which we can leverage to build a sophisticated, composable prompt system directly in the terminal.

The Basic Concept

At its core, gomplate can process a file and replace placeholders with content. We'll use its file.Read function to dynamically inline the contents of any file into our prompt.

For example, you can create a prompt.txt file that asks an AI to analyze your package.json:

Please analyze this package.json file:

{{ file.Read "package.json" }}

What are the main dependencies and what is this project about?

By running a simple command, gomplate renders this template, replacing the placeholder with the actual content of package.json. We can then pipe this fully-formed prompt directly into an AI tool like the Claude CLI.

gomplate -f prompt.txt | claude -p

Building a Composable Prompt Library

The real power comes from creating a library of reusable prompt snippets. You can break down common instructions—like setting a tone or defining an output format—into separate files.

For example, you could create a prompts/ directory with files like:

prompts/tone.txt

<TONE>
Please speak in the tone of a British butler.
</TONE>

prompts/steps.txt

<STEPS>
Please break this down into a series of steps that a junior developer could easily follow.
</STEPS>

You can then compose these snippets into your main prompt, creating a modular and highly reusable system. Your prompt.txt can now be assembled from these pieces:

{{ file.Read "prompts/tone.txt" }}
{{ file.Read "prompts/steps.txt" }}

Please analyze this package.json file:

{{ file.Read "package.json" }}

What are the main dependencies and what is this project about?

This workflow gives you precise control over your AI prompts, allowing you to easily build, reuse, and share complex instructions without the manual effort.

[00:00] We're going to use a project called GOMplate, which is an awesome library for building up templates that are rendered together in the CLI. I installed GOMplate using Homebrew, and if you look at the help you'll see there's a flag available which is "-f for file", which will allow us to process a file as a template. So that means that we can use our prompt as a template. So if I just paste in this prompt, you can use curly brace syntax with a command to inline a file. So if I run gomplate-f with my prompt.txt, you'll see the result will be my text and then the package JSON inlined.

[00:38] Now this will allow us to run that same command and pipe it into Cloud. So cloud dash p hit enter and now you have a package JSON analyzer. So if you have a prompt library we'll create a few prompts. We'll put one in a prompts directory and call it tone.txt and I'm going to XML fence these. Please speak in the tone of a British Butler and then we'll create another prompt called steps.txt.

[01:04] I'm going to fence this as well. Please break this down into a series of steps that a junior developer could easily follow. Then we can go over to our prompt and inline those two files. So we'll say file read prompts tone and file read prompt steps. And this time in our terminal we'll clear this out, run this command again, and you can see this time it's broken down into steps, which was kind of a big ask for such a simple task, but I think it did a great job here.

[01:33] And you can also get a hint of British Butler, a manner that would be most instructive for a junior developer. So we now have this composable prompt system where we have our main prompt. We can write as many prompts as we want, toss in any files that we want, and have precise control over what gets passed into as the prompt for Claude to be able to build up these reusable workflows.