Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested templates and include function #399

Merged
merged 1 commit into from
Feb 25, 2024

Conversation

aywan
Copy link
Contributor

@aywan aywan commented Jan 21, 2022

Hi!

I have the task of including file content in the destination. Now I have a draft template in which I first "render" using bash, and then I use result as a template in docker-gen.

First, I added an import function that returns the contents of a file.
Secondly, I added the ability to parse several templates. To do this, instead of one file, specify several through ;.

I tried to write the documentation, but I think that it must be corrected.

I don’t know how useful this feature will be, but at my case it can be very useful.

just example

header.inc:

# this file is auto generated

author.tmpl:

# created by {{ author.name }}
# email {{ author.email }}

result.tmpl:

{{ include "header.inc" }}
{{ template "author.tmpl" }}
---
some result content

executing: docker-gen "result.tmpl;author.tmpl"
result:

# this file is auto generated
# created by 
# email 
---
some result content

…les for use of standard go 'template' function.
@aywan aywan force-pushed the multi-template branch from 82bf72c to 338283c Compare June 8, 2022 10:36
@buchdag buchdag self-assigned this Dec 3, 2022
@josegonzalez
Copy link
Contributor

Can the include here be used to assign the output to a variable?

@josegonzalez
Copy link
Contributor

@buchdag it would be great to have this functionality, as it would allow out of band specification of domains to be associated with apps (where an app is a specific label on a collection of docker containers).

@josegonzalez
Copy link
Contributor

Actually this could be quite nice if the entire "app" config for a set of containers was stored in a file. You could group all containers by an app label, grab the config from disk, and generate file output as appropriate. If the app config changes, then whatever the thing is that writes the config could SIGHUP docker-gen, allowing the written files to be updated. This would remove the need for rebuilding containers, which is necessary due to Docker not exposing metadata that can be modified.

This wouldn't be as useful for swarm, but one could implement an http_request function that returns a response, allowing users to parse the response as "app" config. In any case, that could definitely be done in a separate PR.

@@ -208,6 +208,8 @@ e75a60548dc9 = 1 # a key can be either container name (nginx) or ID

The templates used by docker-gen are written using the Go [text/template](http://golang.org/pkg/text/template/) language. In addition to the [built-in functions](http://golang.org/pkg/text/template/#hdr-Functions) supplied by Go, docker-gen uses [sprig](https://masterminds.github.io/sprig/) and some additional functions to make it simpler (or possible) to generate your desired output.

For parsing several templates, split path with `;` (for example `template = "nginx.tmpl;header.tmpl"`). This makes possible to use go nested templates through standard `template` function.
Copy link
Member

Choose a reason for hiding this comment

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

We should mention that the "main" template should be first in the list.

@josegonzalez
Copy link
Contributor

@buchdag if the only requested change is a readme change, would it be possible to have this merged and then I can update the readme in a follow-up PR?

@buchdag
Copy link
Member

buchdag commented Feb 25, 2024

@josegonzalez sure 👍

@josegonzalez
Copy link
Contributor

@buchdag docs here: #598

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants