mirror of
https://github.com/foolin/goview.git
synced 2025-10-05 23:06:50 +08:00
Initial commit
This commit is contained in:
44
_examples/basic/main.go
Normal file
44
_examples/basic/main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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)
|
||||
|
||||
}
|
Reference in New Issue
Block a user