• Css class based on loaded template

    I have this bootstrap nav in my _base.html template, like this:

    <ul class="nav navbar-nav"> <li><a href="/" class="">Home</a></li> <li><a href="/blog/">Blog</a></li> </ul> 

    Using Golang, I want to add

     class="active" 

    to the corresponding list item.

    I have read html / template docs and articles like thisone , but it seems to me that I should write a golang function that adds

     class="active" 

    each corresponding list item. But for some reason, all the same, I think it would be cleaner if I could just add something like

     <ul> <li{{ if .template = "index.html" }} class="active"{{ end }}><a href="/">Home</a></li> <li{{ if .template = "blog.html" }} class="active"{{ end }}><a href="/blog/">Blog</a></li> </ul> 

    or something like that. I remember how Rob Pike said that the Golan should do all the calculations for you, but why is there an if if statement in the html / template-package?

    +1
    css go go-html-template
    source share
    3 answers

    I personally often implement a small eq helper for such tasks:

     var tmpl = template.Must(template.New("").Funcs(template.FuncMap{ "eq": func(a, b interface{}) bool { return a == b }, }).ParseGlob("templates/*.html") 

    Usage example:

     <li{{if eq .Active "index"}} class="active"{{end}}><a href="/">Home</a></li> 

    But use it only for the display logic itself. It is good practice to support display logic and real computing.

    +5
    source share

    Currently, you do not need to implement your own eq helper. It is already included in the template package.

     <ul> <li {{if eq .Active "info" }}class="active"{{end}}> <a href="/info">{{.User.Info}}</a> </li> </ul> 

    Now create this template using an anonymous structure.

     // get template from file view := template.Must(template.ParseFiles( "views/info.html", "views/layout.html", )) // render template with data in route handler data := struct { User *User // some custom struct with further details Active string }{ user, // a User instance "info", } err = view.ExecuteTemplate(w, "layout", data) check(err) 
    +2
    source share

    I think this is the way to go in your case. You can reformulate this a little differently, depending on your specific use case, for example:

     type State struct { active string } func (s *State) Class(page string) { if s.active == page { return `class="active"` } return `class="notactive"` // Or whatever default case you want } tmplt = ` <ul class="nav navbar-nav"> <li><a href="/" {{.Class "index"}}>Home</a></li> <li><a href="/blog/" {{.Class "blog"}}>Blog</a></li> </ul>` 

    Or something in these lines, but the if point in the templates is exactly what you need for these kinds of things. Although your Go code will decide that the WHICH page is active, you still need to pass it to your template, and I think that extract needs some kind of if (or call, as I did above) the state you are passing through, though it already contains all the information.

    0
    source share

    All Articles