I am trying to do some creative formatting using text template and wanted to see if there might be a better approach than what I implemented.
I have an array of structs, where each element has a Project and Task. The array is sorted by Project.
type DisplayTask struct {
Project string
Task string
}
I want to output all the tasks within a project without repeating the project name each time and I want the tasks to be indented, for example:
Project1
Task1
Task2
Project2
Task3
Project3
Task4
Task5
After much experimentation, I am using the following template and trying to determine if there is a way to avoid the printf and just have the indent as part of the template, but I can't figure out a way to do this and suppress white space (newline) from the if statement for the same project.
{{- $lastProject := "" -}}
{{ range . }}
{{- if (ne $lastProject .Project) -}}
{{ .Project }} {{ $lastProject = .Project }}
{{ end -}}
{{ printf " %s" .Task }}
{{ end -}}
The challenge I had was suppressing white space after the end, but still allowing an indent via white space, so resorted to the printf.
Is there a better way?