模板中的bson.ObjectId [英] bson.ObjectId in a template
本文介绍了模板中的bson.ObjectId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
type Test结构{
Id bson.ObjectId
名称字符串
Foo字符串
}
我想在html模板中渲染它
{{Name}} {{Food}}
< ; a href =/ remove / {{Id}}>删除我< / a>
但是这显然不起作用,因为 {{Id}} code>只会返回一个ObjectId类型,有没有办法将它转换成模板内的字符串?
或者当我将数据传递给 template.Execute
?
解决方案调用
id.Hex()
将返回 bson.ObjectId
的字符串表示形式。 如果您试图将一个 bson.ObjectId
编组为json字符串,那么这也是默认行为。
I have a struct with a bson.ObjectId type, for example something like this:
type Test struct {
Id bson.ObjectId
Name string
Foo string
}
I want to render this in an html template
{{ Name }} {{ Food }}
<a href="/remove/{{ Id }}">Remove me</a>
But this obviously doesn't work since {{ Id }}
would just return a ObjectId type, is there a way to convert this into a string inside the template?
Or do I have to do this when I pass data to the template.Execute
?
解决方案
Calling id.Hex()
will return a string representation of the bson.ObjectId
.
This is also the default behavior if you try to marshal one bson.ObjectId
to json string.
这篇关于模板中的bson.ObjectId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文