mcncyo
November 3, 2022, 3:49am
1
I am trying to write a little code and I need some help. I know a little bit of go language. I am trying to make if It has the
date: 2021-01-24T18:19:25+06:00 in the .md file to use that date otherwise use the .GitInfo.AuthorDate.
here is my code below
{{if .PublishDate = 0 {
{{.PublishDate.Format " January 02 2006"}}
} else {
{{.GitInfo.AuthorDate.Format "January 02 2006"}}
}
What am I doing wrong?
{{ with .PublishDate }}
{{ .Format "January 02 2006" }}
{{ else }}
{{ with .GitInfo.AuthorDate }}
{{ .Format "January 02 2006" }}
{{ end }}
{{ end }}
Make sure you have set enableGitInfo = true
in your site configuration.
1 Like
mcncyo
November 3, 2022, 4:04am
3
Thanks. That helps a lot.
Can you explain why use “with” instead of “if”?
See https://pkg.go.dev/text/template . Take some time to read the whole thing.
{{with pipeline}} T1 {{end}}
If the value of the pipeline is empty , no output is generated;
otherwise, dot is set to the value of the pipeline and T1 is
executed.
The empty values are false, 0, any
nil pointer or interface value, and any array, slice, map, or
string of length zero.
{{ with 42 }}
{{ . }} --> 42
{{ end }}
Read this too:
https://www.regisphilibert.com/blog/2018/02/hugo-the-scope-the-context-and-the-dot/
The with
action can make your code a bit cleaner, and makes defensive coding simpler. You just need to keep track of the dot.
system
Closed
November 5, 2022, 4:11am
5
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.