go-html-template

css class based on loaded template

泪湿孤枕 提交于 2019-12-24 07:18:47
问题 I've got 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 a class="active" to the corresponding list-item. I've read the html/template docs and articles like thisone, but it appears to me that I have to write a golang function that adds class="active" to every correspondending corresponding list-item. But somehow still I think it would be cleaner if I

Go Template : Use nested struct's field and {{range}} tag together

我的未来我决定 提交于 2019-12-24 06:34:51
问题 I have the following nested struct and I would like to iterate them in a template, in a {{range .Foos}} tag. type Foo struct { Field1, Field2 string } type NestedStruct struct { NestedStructID string Foos []Foo } I'm trying with the following html/template but it can't access the NestedStructID from NestedStruct . {{range .Foos}} { source: '{{.Field1}}', target: '{{.NestedStructID}}' }{{end}} Is there any way with golang templates to do what I'd like to do? 回答1: You can't reach the

Golang. html/template. How to put a unquoted string to <script>?

感情迁移 提交于 2019-12-24 01:46:40
问题 I have a template: <script type="text/template" id="data-user">{{.User}}</script> Where "User" is json string in URL encoding format. Something like %7Bdata%22%3A%5B%7B%7D%7D But default html/template put it inside quotes like "%7Bdata%22%3A%5B%7B%7D%7D" I tried that stuff from html/template godoc reference Context {{.}} After {{.}} O'Reilly: How are <i>you</i>? <a title='{{.}}'> O'Reilly: How are you? <a href="/{{.}}"> O'Reilly: How are %3ci%3eyou%3c/i%3e? <a href="?q={{.}}"> O'Reilly%3a

Simple if not working go template

大憨熊 提交于 2019-12-20 03:19:55
问题 So I am doing a simple if check on a bool from a struct but it doesn't seem to work, it just stop rendering the HTML. So the following struct is like this: type Category struct { ImageURL string Title string Description string isOrientRight bool } Now I have a slice of that Category struct which I get to display with a range. Bellow is an example of one struct: juiceCategory := Category{ ImageURL: "lemon.png", Title: "Juices and Mixes", Description: `Explore our wide assortment of juices and

avoid auto escape in html template

…衆ロ難τιáo~ 提交于 2019-12-20 02:52:42
问题 Trying to render HTML template with an URL. The problem is that the URL contains () in it, and those characters are escaped. I tried to use template.URL("http://myurl.com/(data)/aaa.jpg") and also template.HTML("http://myurl.com/(data)/aaa.jpg") but it still escape brackets. I'm using gin gonic. router.GET("/test", func(c *gin.Context) { c.HTML(http.StatusOK, "test.tmpl", gin.H{ "url": template.URL("http://myurl.com/(data)/aaa.jpg"), // "url": template.HTML("http://myurl.com/(data)/aaa.jpg"),

Html template use on golang

偶尔善良 提交于 2019-12-14 03:32:35
问题 Sorry im beginner and i read golang.docs but didnt understand well. i`ve : index.html: <html> <head> </head> <body> <form action type="checkbox" name="test" value="A" {{.checked}}> <input type="submit" value="save"> </body> </html> in main.go if user click save button then check checkbox redirect that page and show checkbox checked 回答1: You could send variables in map. For example: package main import ( "bytes" "fmt" "text/template" ) func main() { t, _ := template.New("hi").Parse("Hi {{.name

Golang Template Range (for loop) using JSON from WebSocket

久未见 提交于 2019-12-12 01:46:39
问题 I'm using Gorilla Websocket to update some HTML (img src, text, etc); I do this the following way: mt, message, err := c.ReadMessage() if err != nil { log.Println("read:", err) break } [...] app, err := models.DB.SearchAppStore(ctx, stars, updatedWithin, 0) myJson, err := json.Marshal(app) err = c.WriteMessage(mt, myJson) if err != nil { log.Println("write:", err) break } Then I use javascript to update the HTML data this way: ws.onmessage = function(evt) { var d = JSON.parse(evt.data); var

Format float in golang html/template

♀尐吖头ヾ 提交于 2019-12-10 02:17:29
问题 I want to format float64 value to 2 decimal places in golang html/template say in index.html file. In .go file I can format like: strconv.FormatFloat(value, 'f', 2, 32) But I don't know how to format it in template. I am using gin-gonic/gin framework for backend. Any help will be appreciated. Thanks. 回答1: You have many options: You may decide to format the number e.g. using fmt.Sprintf() before passing it to the template execution ( n1 ) Or you may create your own type where you define the

How to compare the length of a list in html/template in golang?

血红的双手。 提交于 2019-12-03 08:32:02
问题 I am trying to compare the length of a list in golang html/template. But it is loading forever in html. {{ $length := len .SearchData }} {{ if eq $length "0" }} Sorry. No matching results found {{ end }} Could anyone help me with this? 回答1: From documentation, {{if pipeline}} T1 {{end}}: If the value of the pipeline is empty, no output is generated; otherwise, T1 is executed. The empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero

avoid auto escape in html template

╄→гoц情女王★ 提交于 2019-12-02 03:18:57
Trying to render HTML template with an URL. The problem is that the URL contains () in it, and those characters are escaped. I tried to use template.URL("http://myurl.com/(data)/aaa.jpg") and also template.HTML("http://myurl.com/(data)/aaa.jpg") but it still escape brackets. I'm using gin gonic . router.GET("/test", func(c *gin.Context) { c.HTML(http.StatusOK, "test.tmpl", gin.H{ "url": template.URL("http://myurl.com/(data)/aaa.jpg"), // "url": template.HTML("http://myurl.com/(data)/aaa.jpg"), }) Template file : <div> <img src="{{.url}}" /> </div> final ouput : <div> <img src="http://myurl.com