Giter VIP home page Giter VIP logo

Comments (5)

breml avatar breml commented on June 14, 2024 1

@forquare Thanks for the additional context. As you have written, include is a function provided by helm (source code: https://github.com/helm/helm/blob/e81f6140ddb22bc99a08f7409522a8dbe5338ee3/pkg/engine/engine.go#L129), which works similar to the function yield that I am proposing in this issue.
In the documentation you linked above there is a statement about template, which explains why your snippet does not work:

Because template is an action, and not a function, there is no way to pass the output of a template call to other functions; the data is simply inserted inline.

The key is "template is an action, and not a function". Additionally to the limitation, that it does not allow the output to be passed to an other function, it is also more strict about the "arguments" it accepts. The first argument (name of the template) is required to be a string constant and it is not possible to pass the name of the template as a variable. The error, the template action produces, if the name is not a string constant (but a variable) is provided in the initial description of this issue.

Both these limitations are removed by either the yield function proposed here or the include function available in helm.

from sprig.

forquare avatar forquare commented on June 14, 2024

Not with template, but we have a pattern like this for includes:

{{ include (printf "%s" $template) . }}

It's certainly not as nice as having a dedicated function, but it works well enough for us.

from sprig.

breml avatar breml commented on June 14, 2024

@forquare Thanks for your contribution. I am not entirely sure I can follow your example. May I ask you to elaborate. In particular what is $template (filename, name of the thing to include, the content to be included (plain text), the content to be included, but rendered as a Go template, something else) and what does the include function exactly do. Thanks.

from sprig.

breml avatar breml commented on June 14, 2024

On a side note: Hugo does provide include functionality for Go templates, see https://gohugo.io/templates/introduction/#includes

from sprig.

forquare avatar forquare commented on June 14, 2024

@breml Firstly, let me apologise because I'm not sure how Helm does this, but I think they do some kind of preprocessing - I've tried this with my limited Go knowledge and can't replicate my suggestion simply using Go text/template.

Secondly, apologies for not expanding. Despite the fact that what I said may not be valid! Let me still expand:

include is a function in Helm which "will import the contents of a template into the present pipeline where it can be passed along to other functions in the pipeline" (from the Helm documentation here)

In my example, $template is a variable containing the name of a named template, exactly the same as your first example. I had thought that your original example could have been rewritten like this:

{{ define "first" }} First {{ end }}
{{ $template := "first" }}
{{ template (printf "%s" $template) . }}

However I think you would need to have some sort of preprocessing in place to handle this, as my limited knowledge cant' replicate this simply in Go.

from sprig.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.