Golang Templates folder ParseGlog tutorial# 4

Golang Templates folder ParseGlog tutorial# 4

We have create 3 html files and 3 routes

  1. index page
  2. help page
  3. contact page

How to serve HTML templates folder ?

 tmpl, _ := template.ParseGlob("templates/*.html")

How to execute templates from templates folder?

tmpl.ExecuteTemplate(w, "help.html", nil)

or

tmpl.ExecuteTemplate(w, "help.html", "data")

 

<div>
<div>package main</div>
<div>import (</div>
<div>    "html/template"</div>
<div>    "net/http"</div>
<div>)</div>
<div>func main() {</div>
<div>    tmpl, _ := template.ParseGlob("templates/*.html")</div>
<div>    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {</div>
<div>        tmpl.ExecuteTemplate(w, "index.html", "data passed to index page")</div>
<div>    })</div>
<div>    http.HandleFunc("/contact", func(w http.ResponseWriter, r *http.Request) {</div>
<div>        tmpl.ExecuteTemplate(w, "contact.html", "data passed to contact Page")</div>
<div>    })</div>
<div>    http.HandleFunc("/help", func(w http.ResponseWriter, r *http.Request) {</div>
<div>        tmpl.ExecuteTemplate(w, "help.html", "data passed to help Page")</div>
<div>    })</div>
<div>    http.ListenAndServe(":8080", nil)</div>
<div>}</div>
</div>

Index html file content

<div>
<div>&lt;html lang="en"&gt;</div>
<div>&lt;head&gt;</div>
<div>    &lt;meta charset="UTF-8"&gt;</div>
<div>    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;</div>
<div>    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;</div>
<div>    &lt;title&gt;GOLANG INDEX HTML&lt;/title&gt;</div>
<div>&lt;/head&gt;</div>
<div>&lt;body&gt;</div>
<div>    INDEX HTML PAGE</div>
<div>    &lt;h1&gt;INDEX HTML PAGE&lt;/h1&gt;</div>
<div>    &lt;h2&gt;INDEX HTML PAGE&lt;/h2&gt;</div>
<div>    &lt;h3&gt;INDEX HTML PAGE&lt;/h3&gt;</div>
<div>    &lt;h4&gt;{{.}}&lt;/h4&gt;</div>
<div>&lt;/body&gt;</div>
<div>&lt;/html&gt;</div>
</div>

Help html file content

<div>
<div>&lt;html lang="en"&gt;</div>
<div>&lt;head&gt;</div>
<div>    &lt;meta charset="UTF-8"&gt;</div>
<div>    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;</div>
<div>    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;</div>
<div>    &lt;title&gt;GOLANG HTML&lt;/title&gt;</div>
<div>&lt;/head&gt;</div>
<div>&lt;body&gt;</div>
<div>    HELP PAGE</div>
<div>    &lt;h1&gt;HELP PAGE&lt;/h1&gt;</div>
<div>    &lt;h2&gt;HELP PAGE&lt;/h2&gt;</div>
<div>    &lt;h3&gt;HELP PAGE&lt;/h3&gt;</div>
<div>    &lt;h4&gt;{{.}}&lt;/h4&gt;</div>
<div>&lt;/body&gt;</div>
<div>&lt;/html&gt;</div>
</div>

Contact html file content

<div>
<div>&lt;html lang="en"&gt;</div>
<div>&lt;head&gt;</div>
<div>    &lt;meta charset="UTF-8"&gt;</div>
<div>    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;</div>
<div>    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;</div>
<div>    &lt;title&gt;GOLANG Contact HTML&lt;/title&gt;</div>
<div>&lt;/head&gt;</div>
<div>&lt;body&gt;</div>
<div>    Contact PAGE</div>
<div>    &lt;h1&gt;Contact PAGE&lt;/h1&gt;</div>
<div>    &lt;h2&gt;Contact PAGE&lt;/h2&gt;</div>
<div>    &lt;h3&gt;Contact PAGE&lt;/h3&gt;</div>
<div>    &lt;h4&gt;{{.}}&lt;/h4&gt;</div>
<div>&lt;/body&gt;</div>
<div>&lt;/html&gt;</div>
</div>
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: