Skip to content

proposal: text/template: allow multiple actions within {{}} #46588

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

Closed
atishay opened this issue Jun 5, 2021 · 9 comments
Closed

proposal: text/template: allow multiple actions within {{}} #46588

atishay opened this issue Jun 5, 2021 · 9 comments

Comments

@atishay
Copy link

atishay commented Jun 5, 2021

What version of Go are you using (go version)?

1.16.4

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOOS="darwin"

What did you do?

The desire is to be able to place multiple commands in a Go text template. We already have newlines supported ( #29770 ). If we get multiple commands using any separator, we can simplify templates with multiple mustaches one after the other.

An example using Hugo (with a semi-colon as a separator, can be anything):

{{
      with site.Title;
          printf "%s" (. | humanize);
     else;
          printf "No title";
     end;
}}

equivalent to:

{{with site.Title}}
    {{printf "%s" (. | humanize)}}
{{else}}
  {{printf "No title"}}
{{end}}
@seankhliao seankhliao changed the title text/templates Support an operator for chaining multiple go-template commands in a single mustache proposal: text/template: allow multiple actions within {{}} Jun 5, 2021
@gopherbot gopherbot added this to the Proposal milestone Jun 5, 2021
@seankhliao
Copy link
Member

cc @robpike

@rsc
Copy link
Contributor

rsc commented Jun 9, 2021

It's unclear why we would take on the added complexity here when there is already a way to write that with more {{ }}.

@rsc
Copy link
Contributor

rsc commented Jun 9, 2021

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@earthboundkid
Copy link
Contributor

Why not use {{- else -}} to make the whitespace work with the existing mechanism?

For the specific case of a simple if expression, Hugo has cond, which works like

{{ cond site.Title (site.Title | humanize) "No title" }}

That may solve your usecase, but note that it eagerly evaluates both alternatives.

@robpike
Copy link
Contributor

robpike commented Jun 10, 2021

I agree that the implementation and specification cost is large for a small benefit. It is not a goal of the template language to be as convenient as possible, but rather just to provide a safe link between macro expansion and Go values.

@atishay
Copy link
Author

atishay commented Jun 10, 2021

Here are a few examples of complex logic encapsulated in templates that might help this discussion:

Most of the embedded templates in Hugo are having a huge amount of template code where it is harder to read with the mustaches around them.

@earthboundkid
Copy link
Contributor

I have written a lot of frontend logic in my Hugo templates myself. I don't find the mustaches to be a big deal, particularly for HTML where whitespace is not significant and gets minimized away. Before you could reassign variables (!), it was a pain to have to use a Hugo scratch for logic, but now that you can, it's basically just a simple dynamic language for processing frontend tags from my point of view, like PHP but more internally consistent.

@rsc
Copy link
Contributor

rsc commented Jun 16, 2021

Based on the discussion above, this proposal seems like a likely decline.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Jul 14, 2021

No change in consensus, so declined.
— rsc for the proposal review group

@rsc rsc closed this as completed Jul 14, 2021
@golang golang locked and limited conversation to collaborators Jul 14, 2022
@rsc rsc moved this to Declined in Proposals Aug 10, 2022
@rsc rsc added this to Proposals Aug 10, 2022
@rsc rsc removed this from Proposals Oct 19, 2022
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

6 participants