-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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 missingkey=ignore #23488
Comments
Change https://golang.org/cl/88596 mentions this issue: |
Once you have
If this means to parse |
Re-process the output later during the final phase of evaluation. In a system that is making two passes over the templates, I don't want to fail silently or be lossy on the first pass, but also don't want to fail immediately, either. I was considering shimming in
The existing testing framework that I'm extending makes use of two passes over a template before evaluating the final result during the execution of the tests. The point about In an ideal world there would be a callback or handler that would be called when Another more blunt approach is two have two sets of delimiters that are used for each phase of processing. This would result in users needing to rewrite templates, which is problematic and a chore, but not impossible. Before going this last route, I thought I'd burn a few cycles and see what the interest was in echoing the AST node and re-running the result through the template engine. |
Given that what you want to do with this new feature doesn't work (as you said) and is very hard to specify in any way that's useful, let's not do this. |
Would having an extra supported type besides struct and map that would resolve a field name help? A type that implements something like I am happy to open a separate issue if this proposal has a chance of being added to |
I don't see why users supplying incompatible data is a good justification for not implementing this feature. The template |
For anyone else looking for this I have implemented a much better solution at https://github.com/sugarkube/texttemplate/ - the change set linked to in this issue didn't support submaps and didn't return the entire tag pipeline (i.e. |
I'd like to propose extending
text/template
to include amissingkey=ignore
Option()
. Sometimes it is desirable to re-parse the template to find the original value and handle or defer processing until all template variables have been provided. In order to do this,text/template
needs to pass through the original, invalid or missing value. Here is what I was thinking:Note that in
want
the extra whitespace between the left and right delimiters that was injected. I'm unsure as to whether or not that is actually more correct vs not adding any additional whitespace.Because
text/template
's lexer eats whitespace, this isn't perfect, but it does eventually reduce to the correct value. Specifically:foo {{- .bar}}
comes back asfoobaz
if the variablebar
was assigned the valuebaz
. If, however,bar
was not part of the variable map, the result would befoo{{ .bar }}
.This limitation seemed acceptable to me. Thoughts?
The text was updated successfully, but these errors were encountered: