“Golang: Usando Gin para renderizar modelos HTML” Respostas de código

Golang renderizar modelo html

// parser html via variable

package main

import (
	"net/http"
	"text/template"
)

type Context struct {
	Title  string
	Name   string
	Fruits [3]string
}

func main() {
	const doc = `
<!DOCTYPE html>
<html>
    <head>
        {{.Title}}
    </head>
    <body>
        <h3>Hi, {{.Name}}. The fruits are:</h3>
        <ul>
            {{range .Fruit}}
                <li>{{.}}</li>
            {{end}}
        </ul>
    </body>
</html>
`

	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		w.Header().Add("Content Type", "text/html")
		templates, _ := template.New("doc").Parse(doc)
		context := Context{
			Title:  "My Fruits",
			Name:   "John",
			Fruits: [3]string{"Apple", "Lemon", "Orange"},
		}
		templates.Lookup("doc").Execute(w, context)
	})
	http.ListenAndServe(":8000", nil)
}
Restu Wahyu Saputra

Golang: Usando Gin para renderizar modelos HTML

func main() {
	router := gin.Default()
	router.LoadHTMLGlob("templates/*.tmpl")
	//router.LoadHTMLFiles("templates/template1.html", "templates/template2.html")
	router.GET("/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.tmpl", gin.H{
			"title": "Main website",
		})
	})
	router.Run(":8080")
}
oyebode amirdeen

Respostas semelhantes a “Golang: Usando Gin para renderizar modelos HTML”

Perguntas semelhantes a “Golang: Usando Gin para renderizar modelos HTML”

Mais respostas relacionadas para “Golang: Usando Gin para renderizar modelos HTML” em HTML

Procure respostas de código populares por idioma

Procurar outros idiomas de código