Skip to content

Instantly share code, notes, and snippets.

@mattkasun
Created October 13, 2021 19:40
Show Gist options
  • Save mattkasun/466c6714fb5d5406edd3f5356eef92f1 to your computer and use it in GitHub Desktop.
Save mattkasun/466c6714fb5d5406edd3f5356eef92f1 to your computer and use it in GitHub Desktop.
Generate Edit Template from Struct
package main
import (
"fmt"
"reflect"
"github.com/gravitl/netmaker/models"
)
func GenerateTemplate(a interface{}, action string) {
v := reflect.ValueOf(a).Elem()
fmt.Printf("{{ template Edit%s }}\n", v.Type())
fmt.Printf("<div id='Edit%s' class=w3-modal>\n", v.Type())
fmt.Println("<div class='w3-modal-content w3-card w3-padding' style='width:50%'>")
fmt.Println("<h2> Edit ", v.Type(), "</h2>")
fmt.Printf("<form action='%s' method=post>\n", action)
for i := 0; i < v.NumField(); i++ {
name := v.Type().Field(i).Name
fmt.Printf("%s:<br><input name=%s type=text value='{{.%s}}'/><br>\n", name, name, name)
}
fmt.Println("<button class='w3-button w3-block w3-padding w3-blue' type=submit>Update ", v.Type(), "</button>")
fmt.Println("<button class='w3-button w3-block w3-padding w3-yellow' type=reset>Reset</button>")
fmt.Println("<button class='w3-button w3-block w3-padding w3-red' onclick=\"document.getElementById('", "Edit", v.Type(), "').style.display='none');\">Cancel</button>")
fmt.Println("</form>")
fmt.Println("</div>")
fmt.Println("</div>")
fmt.Println("{{end}}")
}
func main() {
var u models.Network
ListFields(&u)
fmt.Println("\n ----")
GenerateTemplate(&u, "/ProcessEdit")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment