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

Ternary operator-like function #3860

Closed
jetwash opened this issue Sep 4, 2017 · 3 comments · Fixed by #3868
Closed

Ternary operator-like function #3860

jetwash opened this issue Sep 4, 2017 · 3 comments · Fixed by #3868
Assignees
Milestone

Comments

@jetwash
Copy link

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 as completed in #3868 Sep 8, 2017
bep added a commit that referenced this issue Sep 8, 2017
@danielpost
Copy link

Does this function have any examples/documentation?

@digitalcraftsman
Copy link
Member

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

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants