Documentation ¶
Overview ¶
Package view Created by RTT. Author: teocci@yandex.com on 2022-12월-24
Package view Created by RTT. Author: teocci@yandex.com on 2022-12월-24
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultConfig = Config{ Root: "views", Extension: ".html", Master: "layouts/master", Partials: []string{}, Funcs: make(template.FuncMap), DisableCache: false, Delimiters: Delimiters{Left: "{{", Right: "}}"}, }
DefaultConfig default config
View Source
var HTMLContentType = []string{"text/html; charset=utf-8"}
HTMLContentType representation header is used to indicate the original media type of the resource
Functions ¶
Types ¶
type Config ¶
type Config struct { Root string // view root Extension string // template extension Master string // template master Partials []string // template partial, such as head, foot Funcs template.FuncMap // template functions DisableCache bool // disable cache, debug mode Delimiters Delimiters // delimiters }
Config configuration options
type Delimiters ¶
Delimiters delimiters for template
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine view template engine
func Default ¶
func Default() *Engine
Default new default template engine
Example ¶
//Project structure: //|-- app/views/ // |--- index.html // |--- page.html // |-- layouts/ // |--- footer.html // |--- master.html //render index use `index` without `.html` extension, that will render with master layout. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { err := Render(w, http.StatusOK, "index", 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 := Render(w, http.StatusOK, "page.html", 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)
Output:
func New ¶
New creates a template engine
Example ¶
//Project structure: // //|-- app/views/ // |--- index.tpl // |--- page.tpl // |-- layouts/ // |--- footer.tpl // |--- head.tpl // |--- master.tpl // |-- partials/ // |--- ad.tpl gv := New(Config{ Root: "views", Extension: ".tpl", Master: "layouts/master", Partials: []string{"partials/ad"}, Funcs: template.FuncMap{ "sub": func(a, b int) int { return a - b }, "copy": func() string { return time.Now().Format("2006") }, }, DisableCache: true, }) // Set new instance Use(gv) // Render index use `index` without `.tpl` extension, that will render with master layout. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { err := Render(w, http.StatusOK, "index", 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.tpl` with '.tpl' will only file template without master layout. http.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) { err := Render(w, http.StatusOK, "page.tpl", 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)
Output:
func (*Engine) RenderWriter ¶
RenderWriter render template with io.Writer
func (*Engine) SetFileHandler ¶
func (e *Engine) SetFileHandler(handle FileHandler)
SetFileHandler set file handler
type FileHandler ¶
FileHandler file handler interface
func DefaultFileHandler ¶
func DefaultFileHandler() FileHandler
DefaultFileHandler new default file handler
Click to show internal directories.
Click to hide internal directories.