HTML files templates 9 – Gin Gonic Golang Web Framework

HTML files templates 9 – Gin Gonic Golang Web Framework

 

 

Source Code

<div>
<div>// working with html files and sending data</div>
<div>// dynamic pages</div>
<div>package main</div>
<div>import "github.com/gin-gonic/gin"</div>
<div>func main() {</div>
<div>    // create new router/server</div>
<div>    r := gin.Default()</div>
<div>    // lets create our first route for files</div>
<div>    r.LoadHTMLGlob("./templates/*")</div>
<div>    // it mean all our html template will be in templates folder</div>
<div>    // lets create our Get request routes home, about and contact us</div>
<div>    r.GET("/", func(c *gin.Context) {</div>
<div>        c.HTML(200, "index.tmpl", map[string]string{"title": "home page"})</div>
<div>        // status code // our html file name // data we want to send</div>
<div>        // copy and paste for other 2</div>
<div>    })</div>
<div>    // contact us</div>
<div>    r.GET("/contact", func(c *gin.Context) {</div>
<div>        c.HTML(200, "index.tmpl", map[string]string{"title": "contact us page"})</div>
<div>        // status code // our html file name // data we want to send</div>
<div>        // copy and paste for other 2</div>
<div>    })</div>
<div>    // about us page</div>
<div>    r.GET("/about", func(c *gin.Context) {</div>
<div>        c.HTML(200, "index.tmpl", map[string]string{"title": "about page"})</div>
<div>        // status code // our html file name // data we want to send</div>
<div>        // copy and paste for other 2</div>
<div>    })</div>
<div>    // lets create our template folder and html file</div>
<div>    r.Run()</div>
<div>}</div>
</div>

TAGS#

gin gonic tutorial
gin-gonic / examples
gin-gonic documentation
gin framework tutorial
gin web framework
gin router
golang gin context
gin router group

###Buy Me a Coffee https://www.buymeacoffee.com/khan1

Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: