mirror of
https://github.com/foolin/goview.git
synced 2025-09-27 03:06:23 +08:00
45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
/*
|
|
* Copyright 2018 Foolin. All rights reserved.
|
|
*
|
|
* Use of this source code is governed by a MIT style
|
|
* license that can be found in the LICENSE file.
|
|
*
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/foolin/goview"
|
|
"net/http"
|
|
)
|
|
|
|
func main() {
|
|
|
|
//render index use `index` without `.html` extension, that will render with master layout.
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
err := goview.Render(w, http.StatusOK, "index", goview.M{
|
|
"title": "Index title!",
|
|
"add": func(a int, b int) int {
|
|
return a + b
|
|
},
|
|
})
|
|
if err != nil {
|
|
fmt.Fprintf(w, "Render index error: %v!", err)
|
|
}
|
|
|
|
})
|
|
|
|
//render page use `page.html` with '.html' will only file template without master layout.
|
|
http.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) {
|
|
err := goview.Render(w, http.StatusOK, "page.html", goview.M{"title": "Page file title!!"})
|
|
if err != nil {
|
|
fmt.Fprintf(w, "Render page.html error: %v!", err)
|
|
}
|
|
})
|
|
|
|
fmt.Println("Listening and serving HTTP on :9090")
|
|
http.ListenAndServe(":9090", nil)
|
|
|
|
}
|