Skip to content

proposal: text/template: add a 'missingkey=ignore' option #31147

@boosh

Description

@boosh

I need to use text/template to template a file over successive iterations. Initially not all data values will be present but on later iterations more and more data values will exist.

Currently, values not found in the data supplied to Template.Execute() are replaced depending on the active option (set via Template.Option()). By default the following template:

ID={{ .Id }}, Name={{ .Name }}

will be templated as:

ID=<no value>, Name=Bob

if executed with

data := map[string]interface{}{
    "Name": "Bob",
}

But I need it to return:

ID={{ .Id }}, Name=Bob

Proposal

Adding a 'missingkey=ignore' option that would preserve the original tag in templated output if keys are missing from the data supplied when executing a template. I.e. the above template would return:

ID={{ .Id }}, Name=Bob

if executed with

data := map[string]interface{}{
    "Name": "Bob",
}

This is a duplicate of #23488 but with a different use case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.Proposal

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions