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: allow newlines within actions and pipelines #29770

Open
bep opened this issue Jan 16, 2019 · 7 comments

Comments

Projects
None yet
4 participants
@bep
Copy link
Contributor

commented Jan 16, 2019

This issue resurfaced in gohugoio/hugo#5604 -- but has been mentioned enough times to have proven its value.

In Hugo we have many variadic template functions. One relevant example would be the dict func that accepts a list of key/value pairs.

A current example would look like this:

{{ dict "country" "Norway" "population" "5 millions" "language" "Norwegian" "languageCode" "nb" "weather" "freezing cold" "capitol" "Oslo" "largest_city" "Oslo" "currency"  "Norwegian krone" "dialing_code" "+47" }}

The above would obviously be easier to read if it could be written something like this:

{{ dict 
	"country" "Norway" 
	"population" "5 millions"
	"language" "Norwegian"
	"language_code" "nb"
	"weather" "freezing cold"
	"capitol" "Oslo"
	"largest_city" "Oslo"
	"currency"  "Norwegian krone"
	"dialing_code" "+47" 
}}

The above creates a parser error, and is a common problem when you try to use one of the "HTML beautifiers" out there on Go templates.

As a person who have spent some thousand hours inside Go templates I would say that this issue is really, really worth it. And if you don't come from that background, I think it helps to be reminded that many Go applications needs a user interface, and Go templates is a natural choice. No one questions the value of gofmt and pretty Go code, not sure why the UI code should be treated differently. I know @natefinch tinkered with a "Go template formatter" some time ago. A solution to this partucular issue would make that task more enjoyable, me thinks.

/cc @regisphilibert

@mvdan

This comment has been minimized.

Copy link
Member

commented Jan 16, 2019

This seems reasonable to me. I'm a bit confused by the wording of the title, however. I assume what you want is for newlines to be allowed whithin pipelines - I'm not sure I understand how delimiters play a part here.

I also wonder if there's any disadvantage in allowing newlines in those cases. I can't think of any right now, but then again I haven't written complex pipelines in Go templates.

@mvdan mvdan added the NeedsDecision label Jan 16, 2019

@mvdan mvdan added this to the Go1.13 milestone Jan 16, 2019

@mvdan

This comment has been minimized.

Copy link
Member

commented Jan 16, 2019

Here's a quote from the package godoc:

Except for raw strings, actions may not span newlines, although comments can.

@bep have you given any thought to whether all actions would be allowed to span many newlines? Or would this be restricted to just pipelines, or just commands?

/cc @robpike

@bep

This comment has been minimized.

Copy link
Contributor Author

commented Jan 16, 2019

I'm a bit confused by the wording of the title, however. I assume what you want is for newlines to be allowed whithin pipelines - I'm not sure I understand how delimiters play a part here.

I'm not sure how "what I want" translates to "Go template terms", and I don't remember the difference between commands and pipe. But currently the items between {{ and }} are separated by spaces. If you also allow newline as a separator, it would make many people happy.

@mvdan

This comment has been minimized.

Copy link
Member

commented Jan 16, 2019

Thanks, I understand what you meant now. So I think what you're suggesting is to allow newlines within all actions, which includes pipelines as well as if, range, and everything else that can go between {{ and }}.

@mvdan mvdan changed the title text/template: allow newlines as command delimiter text/template: allow newlines within actions and pipelines Jan 16, 2019

@natefinch

This comment has been minimized.

Copy link
Contributor

commented Jan 16, 2019

strong +1 for this. I can't think of any reason this would cause problems, and I've often wanted to have a template node span more than one line.

As for the template formatter..... I tried and eventually gave up. The stdlib template parser is extremely lossy, so in order to use it for formatting, I was having to dive deep into modifying the parser to be lossless, and that turned into too heavy a lift for the time I had.

@bep

This comment has been minimized.

Copy link
Contributor Author

commented Jan 16, 2019

@mvdan yes, that is correct. So, my primary "pain case" would be the example below. But I suspect that the changes in the template lexer/parser, and its documentation, will not be simpler if you try to somehow narrow the scope.

{{ $mymap := someTemplateFunc 
	"country" "Norway" 
	"population" "5 millions"
	"language" "Norwegian"
	"language_code" "nb"
	"weather" "freezing cold"
	"capitol" "Oslo"
	"largest_city" "Oslo"
	"currency"  "Norwegian krone"
	"dialing_code" "+47" 
}}
@regisphilibert

This comment has been minimized.

Copy link

commented Jan 16, 2019

Mind you, if this simplifies things, this could be limited to certain user defined actions: in the line of {{-, a character attached to the opening curlies could indicate the content of this action accepts newlines:

{{> $mymap := someTemplateFunc 
	"country" "Norway" 
	"population" "5 millions"
	"language" "Norwegian"
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.