If statement 如何在HTML属性中插入if语句?

If statement 如何在HTML属性中插入if语句?,if-statement,go,conditional-statements,go-templates,go-html-template,If Statement,Go,Conditional Statements,Go Templates,Go Html Template,我正在尝试创建一个go html(go 1.13){{.} 我已经设法将上下文变量显示为div值,但是我找不到将上下文变量放入HTML属性中的正确方法 例如,此代码可以工作: {{ if .AppCtx.Title }} <a href="/">{{ .AppCtx.Title }}</a> {{end}} {{if.AppCtx.Title} {{end} 但这似乎不起作用: <a href="{{ if .AppCtx.Link }}{{.AppCtx.L

我正在尝试创建一个go html(go 1.13){{.}

我已经设法将上下文变量显示为div值,但是我找不到将上下文变量放入HTML属性中的正确方法

例如,此代码可以工作:

{{ if .AppCtx.Title }} 
<a href="/">{{ .AppCtx.Title }}</a>
{{end}}
{{if.AppCtx.Title}
{{end}
但这似乎不起作用:

<a href="{{ if .AppCtx.Link }}{{.AppCtx.Link}} {{end}}">

但没有找到任何线索

你是不是打算这样做

<a href="{{ if .AppCtx.Title}}{{.AppCtx.Title}} {{end}}">


什么是“但这似乎不起作用”意思?你说的不起作用的话应该起作用。它做了什么?@Peter my condition是真的,但一旦编译,我的html仅基于该输出显示,
AppCtx.Link
为零或为空
<a href=""{{ if .AppCtx.Link }}{{.AppCtx.Link}} {{end}}>
<a href="{{ if .AppCtx.Link }}{{.AppCtx.Link}} {{end}}">
<a href="{{ if .AppCtx.Title}}{{.AppCtx.Title}} {{end}}">