在Go语言模板中使用null.Time值。

huangapple go评论73阅读模式
英文:

Use null.Time values in a golang template

问题

我正在使用gopkg.in/guregu/null.v4从Postgres数据库中获取一些数据,结果返回得很好,我可以将它们转换为JSON格式,一切都很顺利...然而,我尝试使用模板将结果发送到电子邮件时遇到了问题。

结构部分如下:

type DataQuery struct {
     Date null.Time `json:"DateTime"`
....

模板如下:

{{define "plainBody"}}
你好,

这是今天的检查运行结果。

返回的行数为{{.Rows}}

数据如下:
{{ range .Data}}
    {{.Date}}
{{end}}

{{end}}

运行该模板的结果如下:

你好,

这是今天的检查运行结果。

返回的行数为57

数据如下:

    {{2021-09-13 00:00:00 +0000 +0000 true}}

    {{2021-08-16 00:00:00 +0000 +0000 true}}

    {{2021-09-19 00:00:00 +0000 +0000 true}}

    {{2021-09-18 00:00:00 +0000 +0000 true}}

我尝试使用{{.Date.EncodeText}},结果得到了:

 [50 48 50 49 45 48 57 45 49 51 84 48 48 58 48 48 58 48 48 90]

    [50 48 50 49 45 48 56 45 49 54 84 48 48 58 48 48 58 48 48 90]

    [50 48 50 49 45 48 57 45 49 57 84 48 48 58 48 48 58 48 48 90]

对于日期时间字段(可能是字符串的[]byte,但我不确定),我尝试使用{{Date.Value}},结果得到:

2021-09-13 00:00:00 +0000 +0000

其他字段类型(字符串、整数、浮点数)都可以正常使用:

{{Variable.ValueOrZero}}

我觉得我离成功很近了,但对于日期时间字段还差一点。

英文:

I'm using gopkg.in/guregu/null.v4 to get some data from a Postgres DB and the results are coming back fine, and I can put them into json format and the world is happy... however, I'm trying to email the results using a template and have hit a problem.

The structure is (partially)

type DataQuery struct {
     Date null.Time `json:"DateTime"`
....

The template is

{{define "plainBody"}}
Hi,

Here are the results for the check run for today.

The number of rows returned is {{.Rows}}

The data is
{{ range .Data}}
    {{.Date}}
{{end}}

{{end}}

And the results of running that template are

Hi,

Here are the results for the check run for today.

The number of rows returned is 57

The data is

    {{2021-09-13 00:00:00 +0000 +0000 true}}

    {{2021-08-16 00:00:00 +0000 +0000 true}}

    {{2021-09-19 00:00:00 +0000 +0000 true}}

    {{2021-09-18 00:00:00 +0000 +0000 true}}

I tried using {{.Date.EncodeText}} and ended up with

 [50 48 50 49 45 48 57 45 49 51 84 48 48 58 48 48 58 48 48 90]

    [50 48 50 49 45 48 56 45 49 54 84 48 48 58 48 48 58 48 48 90]

    [50 48 50 49 45 48 57 45 49 57 84 48 48 58 48 48 58 48 48 90]

For the datetime fields (which might be a []byte of the strings but I'm not sure.

If I use {{Date.Value}} I get
2021-09-13 00:00:00 +0000 +0000

The other field types (string, int, float) all work fine with

{{Variable.ValueOrZero}} 

I think I'm close.. but can't quite crack it for the date time fields

答案1

得分: 2

首先,你正在使用html/template,它提供了上下文敏感的转义功能,这就是为什么你看到那些+序列。如果你想要文本输出,请改用text/template。详细信息请参见https://stackoverflow.com/questions/48527115/template-unnecessarily-escaping-to-lt-but-not/48527166#48527166。

接下来,null.Time不仅仅是一个简单的time.Time值,它还包装了其他字段(表示时间是否有效)。当简单地输出它时,有效字段也会被渲染出来(你输出中的true文本)。

你可以只渲染它的Time字段:{{.Date.Time}}

通过这些更改,输出将如下所示:

你好,

这是今天的检查运行结果。

返回的行数为2

数据如下:

    2021-09-20 12:10:00 +0000 UTC

    2021-10-11 13:50:00 +0000 UTC

Go Playground上试一试。

英文:

First, you are using html/template which provides context-sensitive escaping, that's why you're seeing those + sequences. If you want text output, use text/template instead. For details, see https://stackoverflow.com/questions/48527115/template-unnecessarily-escaping-to-lt-but-not/48527166#48527166

Next, null.Time is not just a simple time.Time value, it wraps other fields too (whether the time is valid). When simply outputting it, that valid field will also be rendered (the true texts in your output).

You may render only its Time field: {{.Date.Time}}.

With these changes output will be for example:

Hi,

Here are the results for the check run for today.

The number of rows returned is 2

The data is

    2021-09-20 12:10:00 +0000 UTC

    2021-10-11 13:50:00 +0000 UTC

Try it on the Go Playground.

huangapple
  • 本文由 发表于 2021年9月20日 14:47:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/69250116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定