Go Web编程 Chapter5

Go web编程

Chapter_5 Displaying_Content

context_aware
package main

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

func process(w http.ResponseWriter, r *http.Request) {
   t, _ := template.ParseFiles("tmpl.html")
   content := `I asked: <i>"What's up?"</i>`
   t.Execute(w, content)
}

func main() {
   server := http.Server{
      Addr: "127.0.0.1:8080",
   }
   http.HandleFunc("/process", process)
   server.ListenAndServe()
}

tmpl.html

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Go Web Programming</title>
  </head>
  <body>
    <div>{{ . }}</div>
    <div><a href="/{{ . }}">Path</a></div>
    <div><a href="/?q={{ . }}">Query</a></div>
    <div><a onclick="f('{{ . }}')">Onclick</a></div>
  </body>
</html>
costom_function
package main

import (
   "html/template"
   "net/http"
   "time"
)

func formatDate(t time.Time) string {
   layout := "2006-01-02"
   return t.Format(layout)
}

func process(w http.ResponseWriter, r *http.Request) {
   funcMap := template.FuncMap{"fdate": formatDate}
   t := template.New("tmpl.html").Funcs(funcMap)
   t, _ = t.ParseFiles("tmpl.html")
   t.Execute(w, time.Now())
}

func main() {
   server := http.Server{
      Addr: "127.0.0.1:8080",
   }
   http.HandleFunc("/process", process)
   server.ListenAndServe()
}

tmpl.html

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Go Web Programming</title>
  </head>
  <body>
    <div>The date/time is {{ . | fdate }}</div>
  </body>
</html>
include
package main

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

func process(w http.ResponseWriter, r *http.Request) {
   t, _ := template.ParseFiles("t1.html", "t2.html")
   t.Execute(w, "Hello World!")
}

func main() {
   server := http.Server{
      Addr: "127.0.0.1:8080",
   }
   http.HandleFunc("/process", process)
   server.ListenAndServe()
}

t1.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=9">
    <title>Go Web Programming</title>
  </head>
  <body>    
    <div> This is t1.html before</div>
    <div>This is the value of the dot in t1.html - [{{ . }}]</div>
    <hr/>
    {{ template "t2.html" }}
    <hr/>
    <div> This is t1.html after</div>
  </body>
</html>

t2.html

<div style="background-color: yellow;">
  This is t2.html<br/>
  This is the value of the dot in t2.html - [{{ . }}]
</div>
iterator
package main

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

func process(w http.ResponseWriter, r *http.Request) {
   t, _ := template.ParseFiles("tmpl.html")
   daysOfWeek := []string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
   t.Execute(w, daysOfWeek)
}

func main() {
   server := http.Server{
      Addr: "127.0.0.1:8080",
   }
   http.HandleFunc("/process", process)
   server.ListenAndServe()
}

tmpl.html

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Go Web Programming</title>
  </head>
  <body>
    <ul>
    {{ range . }}
      <li>{{ . }}</li>
    {{ end}}
    </ul>
  </body>
</html>
nested1
package main

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

func process(w http.ResponseWriter, r *http.Request) {
   t, _ := template.ParseFiles("layout.html")
   t.ExecuteTemplate(w, "layout", "")
}

func main() {
   server := http.Server{
      Addr: "127.0.0.1:8080",
   }
   http.HandleFunc("/process", process)
   server.ListenAndServe()
}

layout.html

{{ define "layout" }}

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Go Web Programming</title>
  </head>
  <body>
    {{ template "content" }}
  </body>
</html>

{{ end }}

{{ define "content" }}

Hello World!

{{ end }}
nested2
package main

import (
   "html/template"
   "math/rand"
   "net/http"
   "time"
)

func process(w http.ResponseWriter, r *http.Request) {
   rand.Seed(time.Now().Unix())
   var t *template.Template
   if rand.Intn(10) > 5 {
      t, _ = template.ParseFiles("layout.html", "red_hello.html")
   } else {
      t, _ = template.ParseFiles("layout.html", "blue_hello.html")
   }
   t.ExecuteTemplate(w, "layout", "")
}

func main() {
   server := http.Server{
      Addr: "127.0.0.1:8080",
   }
   http.HandleFunc("/process", process)
   server.ListenAndServe()
}

blue_hello.html

{{ define "content" }}

<h1 style="color: blue;">Hello World!</h1>

{{ end }}

layout.html

{{ define "layout" }}

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Go Web Programming</title>
  </head>
  <body>
    {{ template "content" }}
  </body>
</html>

{{ end }}

red_hello.html

{{ define "content" }}

<h1 style="color: red;">Hello World!</h1>

{{ end }}
random_number
package main

import (
   "html/template"
   "math/rand"
   "net/http"
   "time"
)

func process(w http.ResponseWriter, r *http.Request) {
   t, _ := template.ParseFiles("tmpl.html")
   rand.Seed(time.Now().Unix())
   t.Execute(w, rand.Intn(10) > 5)
}

func main() {
   server := http.Server{
      Addr: "127.0.0.1:8080",
   }
   http.HandleFunc("/process", process)
   server.ListenAndServe()
}

tmpl.html

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Go Web Programming</title>
  </head>
  <body>
    {{ if . }}
      Number is greater than 5!
    {{ else }}
      Number is 5 or less!
    {{ end }}
  </body>
</html>
set_dot
package main

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

func process(w http.ResponseWriter, r *http.Request) {
   t, _ := template.ParseFiles("tmpl.html")
   t.Execute(w, "hello")
}

func main() {
   server := http.Server{
      Addr: "127.0.0.1:8080",
   }
   http.HandleFunc("/process", process)
   server.ListenAndServe()
}

tmpl.html

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Go Web Programming</title>
  </head>
  <body>
    <div>The dot is {{ . }}</div>
    <div>
    {{ with "world"}}
      Now the dot is set to {{ . }}
    {{ end }}
    </div>
    <div>The dot is {{ . }} again</div>
  </body>
</html>
trigger_template
package main

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

func process(w http.ResponseWriter, r *http.Request) {
   t, _ := template.ParseFiles("tmpl.html")
   t.Execute(w, "Hello World!")
}

func main() {
   server := http.Server{
      Addr: "127.0.0.1:8080",
   }
   http.HandleFunc("/process", process)
   server.ListenAndServe()
}

tmpl.html

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Go Web Programming</title>
  </head>
  <body>
    {{ . }}
  </body>
</html>
xss
package main

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

func process(w http.ResponseWriter, r *http.Request) {
   w.Header().Set("X-XSS-Protection", "0")
   t, _ := template.ParseFiles("tmpl.html")
   t.Execute(w, r.FormValue("comment"))
   t.Execute(w, template.HTML(r.FormValue("comment")))
}

func form(w http.ResponseWriter, r *http.Request) {
   t, _ := template.ParseFiles("form.html")
   t.Execute(w, nil)
}

func main() {
   server := http.Server{
      Addr: "127.0.0.1:8080",
   }
   http.HandleFunc("/process", process)
   http.HandleFunc("/", form)
   server.ListenAndServe()
}

form.html

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Go Web Programming</title>
  </head>
  <body>
    <form action="/process" method="post">
      Comment: <input name="comment" type="text" size="50">
     <hr/>
     <button id="submit">Submit</button>
    </form>
  </body>
</html>

tmpl.html

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Go Web Programming</title>
  </head>
  <body>
    <div>{{ . }}</div>
  </body>
</html>
Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy