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

template: Add include function #429

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

atavakoliyext
Copy link

@atavakoliyext atavakoliyext commented Oct 15, 2021

The include function allows templates to be composed of other templates.

The motivation for this change is to allow composition of complex templates from simpler components. This is currently difficult because the only mechanism available for composing a template from multiple files is the fileContents function, which copies contents of files ad-verbatim. This requires that all parameters of the component files are known beforehand, which is often not the case, and does not allow the component templates to use inputs from variables.

Fixes #305

@hashicorp-cla
Copy link

hashicorp-cla commented Oct 15, 2021

CLA assistant check
All committers have signed the CLA.

The include function allows templates to be composed of other templates.

Fixes hashicorp#305
@atavakoliyext
Copy link
Author

Hi @jrasell @angrycub, is this something you would consider including in the project? My company is planning on using levant extensively in our deployment stack and this would be a great help in keeping templates composable & DRY.

I'm happy to make any updates needed to get this in line with the project's standards. Thanks!

@jrasell
Copy link
Member

jrasell commented Nov 15, 2021

@atavakoliyext thanks for this PR; i'll try and get a first pass review done this week!

@jrasell jrasell self-requested a review November 15, 2021 08:45
@jrasell jrasell self-assigned this Nov 15, 2021
@jrasell jrasell added theme/template Relates to templating in general theme/template/function Relates to templating functions type/enhancement labels Nov 15, 2021
@atavakoliyext
Copy link
Author

atavakoliyext commented Nov 15, 2021

@jrasell Thank you!

Copy link
Member

@jrasell jrasell left a comment

Choose a reason for hiding this comment

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

Hi @atavakoliyext and thanks for raising this PR. I have made an initial pass without diving too deep into the code and was curious if you had looked at the go text/template nested definitions feature which would achieve this feature without the need for a custom function. The new nomad-pack implements this feature to allow for helper and partial template rendering in main templates.

@atavakoliyext
Copy link
Author

@jrasell thanks for taking a look. I'm familiar with that feature of text/template, but was unable to find a way to use it in levant without putting those defines in the same files that use them. Since one of motivations of this PR is to let users define reusable snippets elsewhere and use them across multiple jobs, I went with a custom function to give a way to specify the paths to those other files.

Another approach could be to add a new flag for specifying additional template files to load before executing the main template (e.g. a -templates=<dir> that loads any <dir>/_*.tpl files first). That would bring this feature closer to how nomad-pack does it. The downside would be that users would need to be aware of the locations of any additional template files at rendering time, rather than that being a static property of the template itself. nomad-pack doesn't have this downside because the structure of packs enforces where those helpers must exist.

@atavakoliyext
Copy link
Author

Hi @jrasell Do you have any further thoughts on the above?

@dasavick
Copy link

Looking into reducing boilerplate and standardization of database deployment in my nomad jobs. The template function would be so useful!

I don't see myself using levant without that. Maybe some multi-pass wrapper with fileContents, assuming that will work. Just discovered this is HCL1 only, so maybe for ancient nomad versions. Jinja/ansible looks like a way to go at the moment.

It was hard not to notice the project is not maintained anymore. No HCL2 support (#398), really? Not sure what this mysterious CRT onboarding is (#447), but maybe the start of the thing levant need to bounce back.

Bummer.

@dani
Copy link

dani commented Apr 18, 2023

I understand nomad-pack is the futur of jobfile rendering, but, waiting for it to be production ready, any chance to have this feature merged ? Levant is used today, and will likely still be in a few years.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme/template/function Relates to templating functions theme/template Relates to templating in general type/enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Recursively render fileContents
5 participants