Skip to content

Comments

docs: templating best practices#2397

Open
paradisfm wants to merge 3 commits intohairyhenderson:mainfrom
paradisfm:main
Open

docs: templating best practices#2397
paradisfm wants to merge 3 commits intohairyhenderson:mainfrom
paradisfm:main

Conversation

@paradisfm
Copy link

following discussion on issue where i proposed "good templates 101" type docs #2389

i have spent a lot of time with gomplate and thought i would share some things i have learned (and definitely implemented, none of this was realized after the fact...) along the way.

Copy link
Owner

@hairyhenderson hairyhenderson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paradisfm thanks for working on this!

Apologies for the slow response. Overall this is good advice - I have a few very minor suggestions. Hopefully over time this can evolve to include more helpful usage patterns.

@paradisfm
Copy link
Author

@hairyhenderson addressed comments - thanks for the feedback! never written "official" docs before so good to know docs-writing best practices as well :)

paradisfm and others added 3 commits August 26, 2025 10:43
Co-authored-by: Dave Henderson <dhenderson@gmail.com>
Co-authored-by: Dave Henderson <dhenderson@gmail.com>
@github-actions
Copy link

This pull request is stale because it has been open for 60 days with
no activity. If it is no longer relevant or necessary, please close
it. Given no action, it will be closed in 14 days.

If it's still relevant, one of the following will remove the stale
marking:

  • A maintainer can add this pull request to a milestone to indicate
    that it's been accepted and will be worked on
  • A maintainer can remove the stale label
  • Anyone can post an update or other comment
  • Anyone with write access can push a commit to the pull request
    branch

@github-actions github-actions bot added the Stale label Oct 26, 2025
@github-actions github-actions bot closed this Nov 9, 2025
@paradisfm
Copy link
Author

cheeky bump :(

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new documentation guide aimed at helping users write clearer, more maintainable Gomplate templates, based on the discussion in issue #2389.

Changes:

  • Introduces a new “Best Practices in Templating” docs page.
  • Provides practical guidance and examples for managing context, config divergence, conditionals, and reducing repetition.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +2 to +4
title: Best Practices in Templating
weight: 11
menu: main
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weight: 11 conflicts with the existing Usage page (also weight: 11), which can lead to unstable/undesired ordering in the main menu. Consider choosing an unused weight (e.g., 15) so the navigation order is deterministic.

Copilot uses AI. Check for mistakes.
Comment on lines +9 to +11
### Using Environment Variables Effectively

You can and should create your own context using environment variables -- but choose them wisely. They should be overarching umbrellas that templates can share.
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This page uses ### for the top-level section headings. Other pages in docs/content use ## for primary sections (with ### as subsections), which affects the generated TOC/heading hierarchy. Consider changing these top-level headings to ## for consistency.

Copilot uses AI. Check for mistakes.
and set up the context:

```gotemplate
{{- $env := env.Getenv "ENVIRONMENT" -}}
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this example, $PREFIX is referenced but never defined, so the snippet won’t work as-written. Consider defining it (e.g., via env.Getenv "PREFIX" or a literal file:///... base URL) before using it in printf.

Suggested change
{{- $env := env.Getenv "ENVIRONMENT" -}}
{{- $env := env.Getenv "ENVIRONMENT" -}}
{{- $PREFIX := env.Getenv "PREFIX" -}}

Copilot uses AI. Check for mistakes.

### Managing Config Divergence

When dealing with multiple groups of configs that behave differently from one another, you might be tempted to conditional your way out of it:
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammar: “tempted to conditional your way out of it” reads incorrect/unclear. Consider rewording to something like “tempted to use conditionals everywhere” to improve readability.

Suggested change
When dealing with multiple groups of configs that behave differently from one another, you might be tempted to conditional your way out of it:
When dealing with multiple groups of configs that behave differently from one another, you might be tempted to use conditionals everywhere to handle the differences:

Copilot uses AI. Check for mistakes.
"dev1": "cloudprovisioner_iam",
"int2": "cloudprovisioner_iam",
"gov": "cloudprovisioner_iam",
"prod": "{{ index .TF_OUTPUTS_ALL_SERVICES \"cloud_provisioner\" \"db-user-name\" \"value\" }}"
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config.json example is not valid JSON as shown: it’s missing the closing braces for the db_users object and the top-level object (it ends after the prod entry). This makes the example copy/paste fail.

Suggested change
"prod": "{{ index .TF_OUTPUTS_ALL_SERVICES \"cloud_provisioner\" \"db-user-name\" \"value\" }}"
"prod": "{{ index .TF_OUTPUTS_ALL_SERVICES \"cloud_provisioner\" \"db-user-name\" \"value\" }}"
}

Copilot uses AI. Check for mistakes.
**values.gtmpl:**

```gotemplate
{{- defineDatasource "config" "/path/to/config/file" -}}
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This defineDatasource example uses an absolute filesystem path without a file:/// scheme. Per the Datasources docs, the scheme may be omitted for relative paths, but absolute paths should use file:///.... Consider updating the example to avoid a non-working copy/paste.

Suggested change
{{- defineDatasource "config" "/path/to/config/file" -}}
{{- defineDatasource "config" "file:///path/to/config/file" -}}

Copilot uses AI. Check for mistakes.
@github-actions github-actions bot removed the Stale label Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants