0

I'm trying to create a go html (Go 1.13) with a context {{.}}.

I've managed to display context variables as div values, but I can't find the right way to put context variables inside HTML attributes.

For instance, this code works:

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

But this does not seem to work :

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

I' ve tried different syntaxes :

  • single quotes :
<a href='{{ if .AppCtx.Link }}{{.AppCtx.Link}} {{end}}'>
  • double quotes in front of the conditional statement:
<a href=""{{ if .AppCtx.Link }}{{.AppCtx.Link}} {{end}}>
  • double quotes around :
<a href="{{ if .AppCtx.Link }}{{.AppCtx.Link}} {{end}}">

I've read https://golang.org/pkg/html/template/ but didn't find any clue in it.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Fradifrad
  • 53
  • 7

1 Answers1

0

Did you mean to do this instead?

<a href="{{ if .AppCtx.Title}}{{.AppCtx.Title}} {{end}}">
dragunfli
  • 24
  • 3