Date code

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

Thanks. That helps a lot.

Can you explain why use “with” instead of “if”?

See 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

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:

The with action can make your code a bit cleaner, and makes defensive coding simpler. You just need to keep track of the dot.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.