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

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

Closed
boosh opened this issue Mar 29, 2019 · 5 comments
Closed

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

boosh opened this issue Mar 29, 2019 · 5 comments

Comments

@boosh
Copy link

@boosh boosh commented Mar 29, 2019

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.

@boosh boosh changed the title Add an 'identity' option to text/template and html/template Proposal: Add an 'identity' option to text/template and html/template Mar 29, 2019
@gopherbot gopherbot added this to the Proposal milestone Mar 29, 2019
@gopherbot gopherbot added the Proposal label Mar 29, 2019
@boosh boosh changed the title Proposal: Add an 'identity' option to text/template and html/template Proposal: Add a 'missingkey=identity' option to text/template and html/template Mar 29, 2019
@boosh boosh changed the title Proposal: Add a 'missingkey=identity' option to text/template and html/template Proposal: Add a 'missingkey=identity' option to text/template Mar 29, 2019
@agnivade agnivade changed the title Proposal: Add a 'missingkey=identity' option to text/template proposal: text/template: add a 'missingkey=identity' option Mar 30, 2019
@agnivade
Copy link
Contributor

@agnivade agnivade commented Mar 30, 2019

@boosh boosh changed the title proposal: text/template: add a 'missingkey=identity' option proposal: text/template: add a 'missingkey=ignore' option Apr 2, 2019
@boosh
Copy link
Author

@boosh boosh commented Apr 3, 2019

For an implementation see: https://github.com/sugarkube/texttemplate/

@rsc
Copy link
Contributor

@rsc rsc commented Apr 10, 2019

What if Bob's name is not "Bob" but "{{.Id}}"?

This seems the same use case as #23488 and it has the same problem: you can't tell what is "unsubstituted" and what is "substituted but looks like template anyway".

My inclination would be to close this for exactly the same reasons as #23488.

@boosh
Copy link
Author

@boosh boosh commented Apr 11, 2019

What if Bob's name is not "Bob" but "{{.Id}}"?

That should be fully supported behaviour, allowing more complex progressive rendering. E.g.

Name={{ .Name }} -> Name={{ .FullName }} -> Name=Bob Jones

The library would neither need to know nor care that {{ .Name }} was replaced by {{ .FullName }}. On a subsequent pass {{ .FullName }} could be replaced by the corresponding value in a map.

When iteratively rendering templates it'd make sense to be defensive anyway, which would probably mean rendering in several passes - the first x times with this option enabled and finally with it disabled to mop up any outstanding tags.

I don't see why we the library shouldn't include this functionality just because there may be edge cases that cause errors - that applies to most parts of various libraries anyway. This feature would open up additional possibilities though.

@rsc
Copy link
Contributor

@rsc rsc commented Apr 17, 2019

Sorry, but no. This kind of ambiguity about what's the original text and what is substitution is how you end up with "little Bobby tables" problems.

This ambiguity is basically also the same problem as unintended variable capture in macro systems. There is no question that it is powerful to be able to do this - see the entirety of On Lisp, for example - but it also creates many many subtle problems - see all the follow-on papers about hygienic macros in Scheme.

Go is aiming for clear and easy to understand. Especially since most users of templating are using html/template, where original template is trusted more than substituted text, it is critically important to avoid the ambiguity about what came from the original template and what did not. This feature would almost certainly lead to security problem after security problem when used.

@rsc rsc closed this Apr 17, 2019
@golang golang locked and limited conversation to collaborators Apr 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.