go-templates

Golang template and testing for Valid fields

我们两清 提交于 2019-12-01 06:33:24
In Go's database/sql package, there are a bunch of Null[Type] structs that help map database values (and their possible nulls) into code. I'm trying to figure out how to test whether a struct field is null, or in other words, when its Valid property is false. The recommended way to print a SQL field is to use the .Value property, like this: <div>{{ .MyStruct.MyField.Value }}</div> This works great. But suppose I have something slightly more complicated, where I need to test the value against something else, for example: <select name="y"> {{ range .SomeSlice }} <option value="{{ . }}" {{ if eq

Golang template variable isset

帅比萌擦擦* 提交于 2019-12-01 04:13:34
I have created a function to check if a variable is defined: fm["isset"] = func(a interface{}) bool { if a == nil || a == "" || a == 0 { fmt.Println("is not set") return false } fmt.Println("is set") return false } tmpl := template.Must(template.New("").Funcs(fm).ParseFiles("templates/header.html")) err := tmpl.ExecuteTemplate(w, "header", templateData) In the template I have: {{ if isset .Email }} email is set {{ end }} This function works if the variable is contained by the templateData (which is a custom struct that contains a map and a string), but it gives me an error if the variable

Go template remove the last comma in range loop

孤人 提交于 2019-11-30 19:36:32
I have code like this: package main import ( "text/template" "os" ) func main() { type Map map[string]string m := Map { "a": "b", "c": "d", } const temp = `{{range $key, $value := $}}key:{{$key}} value:{{$value}},{{end}}` t := template.Must(template.New("example").Parse(temp)) t.Execute(os.Stdout, m) } it will output : key:a value:b,key:c value:d, but I want something like this: key:a value:b,key:c value:d I don't need the last comma, how to remove it. I found a solution for looping an array here: https://groups.google.com/d/msg/golang-nuts/XBScetK-guk/Bh7ZFz6R3wQJ , but I can't get index for

In a template how do you access an outer scope while inside of a “with” or “range” scope?

我怕爱的太早我们不能终老 提交于 2019-11-30 07:54:05
When inside a with or range , the scope of . is changed. How do you access the calling scope? Testuser {{with .Inner}} Outer: {{$.OuterValue}} Inner: {{.InnerValue}} {{end}} $ is documented in the text/template docs: When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot. You can save the calling scope with a variable: {{ $save := . }} {{ with .Inner }} Outer: {{ $save.OuterValue }} Inner: {{ .InnerValue }} {{ end }} 来源: https://stackoverflow.com/questions/14800204/in-a-template-how-do-you-access-an-outer-scope-while-inside-of-a-with-or

Switch or if/elseif/else inside golang HTML templates

徘徊边缘 提交于 2019-11-30 01:23:25
I have this struct : const ( paragraph_hypothesis = 1<<iota paragraph_attachment = 1<<iota paragraph_menu = 1<<iota ) type Paragraph struct { Type int // paragraph_hypothesis or paragraph_attachment or paragraph_menu } I want to display my paragraphs in a Type dependent way. The only solution I found was based on dedicated functions like isAttachment testing the Type in Go and nested {{if}} : {{range .Paragraphs}} {{if .IsAttachment}} -- attachement presentation code -- {{else}}{{if .IsMenu}} -- menu -- {{else}} -- default code -- {{end}}{{end}} {{end}} In fact I have more types, which makes

Go - HTML comments are not rendered

点点圈 提交于 2019-11-29 14:25:23
I'm building go web application. I found some anomaly on the rendered html page. All of my html comments <!-- --> are suddenly not being rendered. My guess it's because the go version I used (just updated to higher version), because it was fine before I updated it. This is my code: <!-- prepare the breadcrumbs --> <ul class="breadcrumb" data-bind="foreach: viewModel.breadcrumbs"> <!-- ko if: ($index() + 1) < len(viewModel.breadcrumbs()) --> <li> <a data-bind="attr: { href: href }"> <i class="fa fa-home"></i> <span data-bind="text: title"></span> </a> </li> <!-- /ko --> <!-- ko if: ($index() +

In a template how do you access an outer scope while inside of a “with” or “range” scope?

感情迁移 提交于 2019-11-29 10:37:45
问题 When inside a with or range , the scope of . is changed. How do you access the calling scope? 回答1: {{with .Inner}} Outer: {{$.OuterValue}} Inner: {{.InnerValue}} {{end}} $ is documented in the text/template docs: When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot. 回答2: You can save the calling scope with a variable: {{ $save := . }} {{ with .Inner }} Outer: {{ $save.OuterValue }} Inner: {{ .InnerValue }} {{ end }} 来源: https:/

Switch or if/elseif/else inside golang HTML templates

给你一囗甜甜゛ 提交于 2019-11-28 22:12:34
问题 I have this struct : const ( paragraph_hypothesis = 1<<iota paragraph_attachment = 1<<iota paragraph_menu = 1<<iota ) type Paragraph struct { Type int // paragraph_hypothesis or paragraph_attachment or paragraph_menu } I want to display my paragraphs in a Type dependent way. The only solution I found was based on dedicated functions like isAttachment testing the Type in Go and nested {{if}} : {{range .Paragraphs}} {{if .IsAttachment}} -- attachement presentation code -- {{else}}{{if .IsMenu}}

Call a method from a Go template

瘦欲@ 提交于 2019-11-28 17:46:59
Let's say I have type Person struct { Name string } func (p *Person) Label() string { return "This is " + p.Name } How can I use this method from a html/template ? I would need something like this in my template: {{ .Label() }} tux21b Just omit the parentheses and it should be fine. Example: package main import ( "html/template" "log" "os" ) type Person string func (p Person) Label() string { return "This is " + string(p) } func main() { tmpl, err := template.New("").Parse(`{{.Label}}`) if err != nil { log.Fatalf("Parse: %v", err) } tmpl.Execute(os.Stdout, Person("Bob")) } According to the

Golang code to repeat an html code n times

余生颓废 提交于 2019-11-28 02:06:31
I am working on golang web app. In that I need to iterate an HTML line n number of times. func index(w http.ResponseWriter, r *http.Request) { tmpl := template.Must(template.ParseFiles("templates/index.html")) n := 5 tmpl.Execute(w, n) } <ul> <li><a href="/?page=1">1</a></li> <li><a href="/?page=2">2</a></li> . . . <li><a href="/?page=n">n</a></li> </ul> How can I implement this? To repeat something in Go templates, you may use the {{range}} action. But the {{range}} action expects something it can iterate over, e.g. a slice, array or map. Passing a zero-value slice So you have to feed that.