[SOLVED] Preventing dates being escaped

Hi everyone,

I’ve been (w)racking my brains and searching for ages. My dates in the HTML source are being escaped, and I can’t figure out how to suppress it.

Front matter from a typical post:

---
title: "Hello world"
date:  "2016-09-26T16:43:00+10:00"
---

And the rendered output:

<time datetime="2016-09-26T16:43:00&#43;10:00">[..]</time>

I’ve tried the following in my theme file without success:

{{ .Date.Format "2006-01-02T15:04:05-07:00" }}
{{ .Date.Format "2006-01-02T15:04:05-07:00" | safeHTML }}
{{ safeHTML ( .Date.Format "2006-01-02T15:04:05-07:00" ) }}

I even tried this horrible hack this without success :wink:

{{ .Date.Format "2006-01-02T15:04:05-07:00" | replace "&#43;" "+" }}

I’m almost certain this is PEBKAC and “this guy isn’t used to golang dates”, but any pointers would be appreciated.

Cheers

1 Like

Untested, but try outputting the entire attribute definition:

<time {{ printf "datetime=%s" (.Date.Format "2006-01-02T15:04:05-07:00") | safeHTML }}>...</time>

Go templates is very opinionated.

Thanks for that. I tried, and got this strange output:

<time ZgotmplZ>[..]</time>

I tried with just the bare string in the printf:

<time timedate="{{ printf "%s" (.Date.Format "2006-01-02T15:04:05-07:00") | safeHTML }}"></time>

But I get the same escaped HTML for the timezone again. The plot thickens.

<time datetime="2016-09-26T16:43:00&#43;10:00">[..]</time>

The only thing I’ve found that works so far is using a timezone identifier instead:

<time timedate="{{ .Date.Format "2006-01-02T15:04:05MST") | safeHTML }}"></time>

Which returns:

<time datetime="2016-09-26T16:43:00AEST">[..]</time>

But that isn’t valid for the <time> element, or the opengraph tags I want to use.

I had to go refresh my memory. Go wants to output the entire tag so that it knows the full context of the components.

{{ printf "<time datetime=\"%s\">" (.Date.Format "2006-01-02T15:04:05-07:00") | safeHTML }}
2 Likes

You sir, are a living legend. That did the trick, thanks.

For people coming across this thread from a search, this was my full <time> tag including the human-readable text between and some schema RDF:

{{ printf "<time property=\"datePublished\" datetime=\"%s\">" (.Date.Format "2006-01-02T15:04:05-07:00") | safeHTML }}
    {{ .Date.Format "Monday 02 January 2006" }}</time>
2 Likes