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

Ternary operator-like function #3860

Closed
jetwash opened this Issue Sep 4, 2017 · 2 comments

Comments

Projects
None yet
4 participants
@jetwash

jetwash commented Sep 4, 2017

TL;DR: I'd like a function similar to the ternary operator in C-like languages.

One thing I've found a bit frustrating with go templates is that you can't really conditionally assign to a variable (other than with default, which is great, but limited). For example, let's say that for a certain page type I want to use the parent's Params for some stuff in my template. Currently, AFAIK I'd need to use a scratch variable:

{{ if eq .Type "assertion" }}
  {{ .Scratch.Set "pageParams" .Parent.Params }}
{{ else }}
  {{ .Scratch.Set "pageParams" .Params }}
{{ end }}
{{ $pageParams := .Scratch.Get "pageParams" }}

What I'd like to do is something like

{{ $pageParams := ternary (eq .Type "assertion") .Parent.Params .Params }}

I'm not overly fond of the name "ternary" since not everyone writing Hugo themes is likely to be familiar enough with a C-like language to even know about the ternary operator. Maybe "when" would make sense, or "cond"? I don't really care about the name, though.

@bep bep added the Enhancement label Sep 8, 2017

@bep bep added this to the v0.27 milestone Sep 8, 2017

@bep bep self-assigned this Sep 8, 2017

bep added a commit to bep/hugo that referenced this issue Sep 8, 2017

@bep bep closed this in #3868 Sep 8, 2017

bep added a commit that referenced this issue Sep 8, 2017

@danielpost

This comment has been minimized.

Show comment
Hide comment
@danielpost

danielpost Nov 5, 2017

Does this function have any examples/documentation?

danielpost commented Nov 5, 2017

Does this function have any examples/documentation?

@digitalcraftsman

This comment has been minimized.

Show comment
Hide comment
@digitalcraftsman
Member

digitalcraftsman commented Nov 5, 2017

@danielpost there's a pending pull request for the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment