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

text/template: with on not existing key in map produce error with missingkey=error #13158

Closed
zazab opened this issue Nov 5, 2015 · 1 comment

Comments

@zazab
Copy link

zazab commented Nov 5, 2015

It looks like a bug, that with action in templates produce error with missingkey=error.
Due the documentation with action is made for checking if pipeline is empty, and perfectly fits in logic, for example, if foo has key "bar", then print something. But with missingkey=error option it fails all template generation with error. (http://play.golang.org/p/kmmkkrHZYI)

I found a workaround for this case, using index function. It works as expected, and does not produce errors on not existent key. But it makes templates less readable, and, I think, simple addressing should have same behaviour.

@adg
Copy link
Contributor

adg commented Nov 5, 2015

This is exactly how it should work. The with action does not check for the presence of a field, but rather whether the field's value is non-zero. See this complementary example that uses a struct instead of a map: http://play.golang.org/p/b_Opy4892i

@adg adg closed this as completed Nov 5, 2015
@adg adg changed the title with on not existing key in map produce error with missingkey=error tex/template: with on not existing key in map produce error with missingkey=error Nov 5, 2015
@adg adg changed the title tex/template: with on not existing key in map produce error with missingkey=error text/template: with on not existing key in map produce error with missingkey=error Nov 5, 2015
@golang golang locked and limited conversation to collaborators Nov 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants