The use of if statements inside templates really is puzzling me.
I'm trying to put a class = "active"
inside a nav list made with golang templates, to do a basic tab menu that detects the active tab.
Here's my attempt :
{{define "header"}}
<!DOCTYPE html>
<html>
<head>
<title>Geoprod</title>
{{template "stylesheet" .}}
</head>
<body>
<nav class="navbar" role="navigation">
<div class="navbar-header">
<a{{if eq .Active "accueil"}} class="active"{{end}} href="/">Geoprod</a>
</div>
<div class="navbar-body">
<ul class="navbar-list">
<li{{if eq .Active "societe"}} class="active"{{end}}><a href="/societe">Société</a></li>
<li{{if eq .Active "dossier"}} class="active"{{end}}><a href="/dossier">Dossier</a></li>
<li{{if eq .Active "temps"}} class="active"{{end}}><a href="/temps">Temps</a></li>
<li{{if eq .Active "mails"}} class="active"{{end}}><a href="/mails">Mails</a></li>
</ul>
</div>
</nav>
{{end}}
And in main.go :
var FuncMap = template.FuncMap{
"eq": func(a, b interface{}) bool {
return a == b
},
}
var templates = template.Must(template.ParseGlob("templates/*.html"))
and in func main()
templates.Funcs(FuncMap)
The program compiles, but i've found out adding the {{if eq .Active "something"}} class="active"{{end}}
(^^ which I included here) causes the program to not display any text anymore. Any idea why?
eq
is lacking. Please have a look here: golang.org/ref/spec#Comparison_operators Especially the part with comparing interface values. – Tiphanieclass="active"
). The header template is in the file header.html, should I also change the name of the template ? – Ninevehtemplates.New("").Funcs(...)
, but here my templates are already named from within templates, So i can't use templates.New(), can I ? What would be a correct syntax then ? – Nineveh.Active
? I copied it from here, but i really can't find anything about it in the official template doc. – Nineveh.
is your pipeline (on the top level whatever you passed to theExecute*()
methods as the data). If.Active
is not set, this might already be your problem. But this indicates you are not checking yourExecute
error. Please show the code where youExecute()
. – Tiphanieexecuting "header" at <.Active>: Active is not a field of struct type *main.Page
. I just thought.Active
was some sort of built-in, of course it's not in the doc. I'll replace .Active with a field i actually have, and come back to tell you if that works. – NinevehFuncMap
. There is in-build functioneq
inhtml/template
. And what is the value of.Active
? Itmust be a string. – Briefs