Golang : How to use if, eq and print properly in html template
Alright, just a simple problem that took me a while to get it right. Putting the solution down here for my own future reference. The problem that I was trying to solve is to print out the word active
whenever the URL segment match certain word. For instance,
http://domain.com/addrow
the URL first segment is the word addrow
and therefore in the generated HTML code should have the <li class="active">
for Add Row
. Such as this
<ul>
<li class="active"><a href="/addrow" class="">Add Row</a></li>
<li class=""><a href="/deleterow" class="">Delete Row</a></li>
</ul>
To do this properly with html template language in Golang. First, create a function to get the current URI.
// GetURISegment - return URL item from a given position
func GetURISegment(r *http.Request, position int) string {
uriSegments := strings.Split(r.URL.Path, "/")
return uriSegments[position]
}
and follow by
templateDataMap["CurrentSegment"] = GetURISegment(r, 1)
// my own stuff, you should use your own
var dashboardPageNavigation = template.Must(template.New("dashboardPageNavigation").Parse(navigation.NavigationHTML))
the templateDataMap variable will be passed to NavigationHTML template.
In the NavigationHTML, check if the current URI segment name equals to the word that matches the navigation item names. If yes, print out the word active
<ul>
<li class="{{if(eq .CurrentSegment "addproperty")}}{{print "active"}}{{else}}{{print ""}}{{end}}"><a href="/addproperty" class="">Add Property</a></li>
<li class="{{if(eq .CurrentSegment "deleteproperty")}}{{print "active"}}{{else}}{{print ""}}{{end}}"><a href="/deleteproperty" class="">Delete Property</a></li>
</ul>
Too hard to read?
<ul>
<li class="{{if(eq .CurrentSegment "addproperty")}}
{{print "active"}}
{{else}}
{{print ""}}
{{end}}">
<a href="/addproperty" class="">Add Property</a>
</li>
<li class="{{if(eq .CurrentSegment "deleteproperty")}}
{{print "active"}}
{{else}}
{{print ""}}
{{end}}">
<a href="/deleteproperty" class="">Delete Property</a>
</li>
</ul>
Hope this helps and happy coding!
References:
https://socketloop.com/tutorials/golang-get-uri-segments-by-number-and-assign-as-variable-example
https://golang.org/pkg/text/template/#hdr-Functions <-- text/template but applicable to html/template as well
https://stackoverflow.com/questions/44240003/how-to-add-conditional-sentences-in-template
See also : Golang : Populate dropdown with html/template example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.7k PHP : Convert(cast) bigInt to string
+9.3k Golang : Meaning of omitempty in struct's field tag
+13.1k Golang : ROT47 (Caesar cipher by 47 characters) example
+13.6k Golang : Accurate and reliable decimal calculations
+12.8k Golang : How to check if your program is running in a terminal
+18.2k Golang : Example for DSA(Digital Signature Algorithm) package functions
+19.1k Golang : How to force compile or remove object files first before rebuild?
+8k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+28.4k Golang : How to verify uploaded file is image or allowed file types
+4.1k Golang : Convert lines of string into list for delete and insert operation
+4.8k Cash Flow : 50 days to pay your credit card debt
+10k How to test Facebook App on localhost ?