view

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2022 License: MIT Imports: 10 Imported by: 0

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

func Render

func Render(w http.ResponseWriter, status int, name string, data interface{}) error

Render render view template with default instance

func Use

func Use(engine *Engine)

Use setting default instance engine

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

type Delimiters struct {
	Left  string
	Right string
}

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

func New(config Config) *Engine

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) Render

func (e *Engine) Render(w http.ResponseWriter, statusCode int, name string, data interface{}) error

Render render template with http.ResponseWriter

func (*Engine) RenderWriter

func (e *Engine) RenderWriter(w io.Writer, name string, data interface{}) error

RenderWriter render template with io.Writer

func (*Engine) SetFileHandler

func (e *Engine) SetFileHandler(handle FileHandler)

SetFileHandler set file handler

type FileHandler

type FileHandler func(config Config, tplFile string) (content string, err error)

FileHandler file handler interface

func DefaultFileHandler

func DefaultFileHandler() FileHandler

DefaultFileHandler new default file handler

type M

type M map[string]interface{}

M map interface for data

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL