<p>Using a struct with a field name that matches a template function (I think) causes the template to raise an error during rendering. Is there a way to display the attribute other that renaming it?</p>
<p>Example struct:</p>
<pre><code>type MyObject struct {
ObjectId int `form:"objectId" json:"objectId"`
Type string `form:"type" json:"type"`
}
</code></pre>
<p>Example template:</p>
<pre><code><div>Objects:</div>
<ul>
{# range $obj := .Objects #}
<li>{# $obj.Type #} - ({# $obj.ObjectId #})</li>
{# end #}
</ul>
</code></pre>
<p>Edit: including error output by template</p>
<pre><code>template: example.tpl:231:58: executing "content" at <$obj.Type>: Type has arguments but cannot be invoked as function
</code></pre>
<hr/>**评论:**<br/><br/>hipone: <pre><p>Works for me.</p>
<p><a href="https://play.golang.org/p/KaxuIPhiEa" rel="nofollow">https://play.golang.org/p/KaxuIPhiEa</a></p></pre>mbondfusion: <pre><p>Thanks...I see my mistake now. I had this:</p>
<pre><code>{{ $obj.Type ($obj.ObjectId) }}
</code></pre>
<p>Instead of this:</p>
<pre><code>{{ $obj.Type }} ({{ $obj.ObjectId }})
</code></pre>
<p>..Not enough brackets (and thanks for not pointing out my custom brackets :). I use Handlebars along with golang and it uses the same brackets, so I use custom golang template tokens.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传