Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
proposal: text/template: add a 'missingkey=ignore' option #31147
I need to use
Currently, values not found in the data supplied to
will be templated as:
if executed with
But I need it to return:
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:
if executed with
This is a duplicate of #23488 but with a different use case.
That should be fully supported behaviour, allowing more complex progressive rendering. E.g.
The library would neither need to know nor care that
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.
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.