render

package
Version: v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2019 License: MIT, MIT Imports: 6 Imported by: 1

README

Render Http响应渲染器

定义了常用的http响应输出,以及用于渲染的引擎

功能

  • 常用的Html/json输出帮助函数
  • 渲染引擎驱动接口
  • 便于序列化为配置文件的视图管理

使用方式

直接输出

模块中提供了一些方表使用的标准输出工具

//直接将对象序列化为JSON输出
//Content-type将设为"application/json"
render.MustJSON(w,v,200)

//将数据作为HTML输出。
//Content-type将设为"text/html"
render.MustHTML(w,data,200)

//将文件内容作为HTML输出
//Content-type将设为"text/html"
render.MustHTMLFile(w,"/tmp/file.html",200)

//输出标准的HTML状态信息
//Content-type将设为"text/plain"
render.MustError(w,401)
Renderer 渲染器

渲染器是渲染的主要工具

renderer=render.New()

//调用render库的直接输出
renderer.MustJSON(w,v,200)
renderer.MustHTML(w,data,200)
renderer.MustHTMLFile(w,"/tmp/file.html",200)
renderer.MustError(w,401)

//渲染器配置
oc:=render.NewOptionCommon()
oc.Engine=jetengine
oc.viewroot="/tmp"
err=oc.ApplyTo(renderer)
视图

视图是渲染器的基本输出单元

//创建视图配置
viewconfig:=render.NewViewConfig("1.tmpl","2.tmpl")
//视图可以先获取后初始化
view:=renderer.GetView("viewname)
//通过配置初始化视图
view:=renderer.NewView("viewname",viewconfig)

//直接渲染为html输出
//Content-type将设为"text/html"
view.MustRender(w,data)

//渲染为指定状态码的html输出
//Content-type将设为"text/html"
view.MustRenderError(w,data,200)

//将视图渲染为[]byte数据,再手动处理
byteSlice=view.MustRenderBytes(data)
视图配置文件
视图配置文件可以通过配置文件的方式批量设置视图

#TOML版本,其他版本可以根据对应格式配置
#全局开发模式开关。设为开发模式的视图每次都会重新渲染
DevelopmentMode=false
[Views.index]
#开发者模式开关
DevelopmentMode=true
#视图列表
Files=["views/index.tmpl"]
[Views.news]
#开发者模式开关
DevelopmentMode=true
#视图列表
Files=["views/news.tmpl"

使用方式

views:=&ViewsOptionCommon{}
err=toml.Unmarshal(data,views)
views.Init(renderer)
视图数据

视图数据为一个通用的传递给视图的数据结构

使用方式:

data:=render.NewData()

//设置数据
data.Set("value1","123)
//获取数据
v:=data.Get("value1")
//删除数据
data.Del("value1")

//合并数据
data.Merge(data1)
可用渲染引擎
  • gotemplate 基于golang http template的渲染引擎
  • jet 基于 github.com/CloudyKit/jet 的渲染引擎

Documentation

Index

Constants

View Source
const (
	// ContentBinary header value for binary data.
	ContentBinary = "application/octet-stream"
	// ContentHTML header value for HTML data.
	ContentHTML = "text/html"
	// ContentJSON header value for JSON data.
	ContentJSON = "application/json"
	// ContentJSONP header value for JSONP data.
	ContentJSONP = "application/javascript"
	// ContentLength header constant.
	ContentLength = "Content-Length"
	// ContentText header value for Text data.
	ContentText = "text/plain"
	// ContentType header constant.
	ContentType = "Content-Type"
	// ContentXHTML header value for XHTML data.
	ContentXHTML = "application/xhtml+xml"
	// ContentXML header value for XML data.
	ContentXML = "text/xml"
)

const from https://github.com/unrolled/render/blob/v1/render.go

Variables

View Source
var ErrRegisterFuncNotSupported = errors.New("render:error register func not supported")

ErrRegisterFuncNotSupported raised when register func is not supported by engine.

View Source
var ErrTooManyViewFiles = errors.New("error too many view files")

ErrTooManyViewFiles error rasied when too many view files is given. raised by render engine.

View Source
var ErrViewNotExist = errors.New("ErrorViewNotExist")

ErrViewNotExist error which raised when view not exist.

Functions

func Error

func Error(w http.ResponseWriter, status int) (int, error)

Error write a http error to response Return bytes length wrote. Panic if any error raised.

func HTMLFile

func HTMLFile(w http.ResponseWriter, path string, status int) (int, error)

HTMLFile write content of given file to response as html. Return bytes length wrote and any error if raised.

func JSON

func JSON(w http.ResponseWriter, data interface{}, status int) (int, error)

JSON marshal data as json and write to response Return bytes length wrote and any error if raised.

func MustError

func MustError(w http.ResponseWriter, status int) int

MustError write a http error to response Return bytes length wrote. Panic if any error raised.

func MustHTMLFile

func MustHTMLFile(w http.ResponseWriter, path string, status int) int

MustHTMLFile write content of given file to response as html. Return bytes length wrote. Panic if any error raised.

func MustJSON

func MustJSON(w http.ResponseWriter, data interface{}, status int) int

MustJSON marshal data as json and write to response Return bytes length wrote. Panic if any error raised.

func MustWriteHTML

func MustWriteHTML(w http.ResponseWriter, data []byte, status int) int

MustWriteHTML write html data to response. Return bytes length wrote. Panic if any error raised.

func MustWriteJSON

func MustWriteJSON(w http.ResponseWriter, data []byte, status int) int

MustWriteJSON write json data to response. Return bytes length wrote. Panic if any error raised.

func WriteHTML

func WriteHTML(w http.ResponseWriter, data []byte, status int) (int, error)

WriteHTML write html data to response. Return bytes length wrote and any error if raised.

func WriteJSON

func WriteJSON(w http.ResponseWriter, data []byte, status int) (int, error)

WriteJSON write json data to response. Return bytes length wrote and any error if raised.

Types

type CompiledView

type CompiledView interface {
	//Execute execute view with given render data.
	//Return render result as []byte and any error if raised.
	Execute(data interface{}) ([]byte, error)
}

CompiledView complied view interface.

type Data

type Data map[string]interface{}

Data render data struct.

func NewData

func NewData() *Data

NewData create new render data.

func (*Data) Del

func (d *Data) Del(key string)

Del delete data field value by key.

func (*Data) Get

func (d *Data) Get(key string) interface{}

Get Get data field value by key.

func (*Data) Merge

func (d *Data) Merge(data *Data)

Merge merger two render data.

func (*Data) Set

func (d *Data) Set(key string, data interface{})

Set set data field value by key.

type Engine

type Engine interface {
	//SetViewRoot set view root path
	SetViewRoot(string)
	//Compile complie view files to complied view.
	Compile(config *ViewConfig) (CompiledView, error)
	//RegisterFunc register func to engine
	//Return any error if raised.
	RegisterFunc(name string, fn interface{}) error
}

Engine render engine

type NamedView

type NamedView struct {
	//Name view name.
	Name string
	//Renderer view renderer.
	Renderer *Renderer
}

NamedView named view main struct.

func (*NamedView) MustRender

func (v *NamedView) MustRender(writer http.ResponseWriter, data interface{}) int

MustRender render view with given data to response. Return bytes length wrote. Panic if any error raised.

func (*NamedView) MustRenderBytes

func (v *NamedView) MustRenderBytes(data interface{}) []byte

MustRenderBytes render view with given data to bytes. Return bytes length wrote. Panic if any error raised.

func (*NamedView) MustRenderError

func (v *NamedView) MustRenderError(writer http.ResponseWriter, data interface{}, status int) int

MustRenderError render view with given data and status code to response. Return bytes length wrote. Panic if any error raised.

func (*NamedView) Render

func (v *NamedView) Render(writer http.ResponseWriter, data interface{}) (int, error)

Render render view with given data to response. Return bytes length wrote and any error if raised.

func (*NamedView) RenderBytes

func (v *NamedView) RenderBytes(data interface{}) ([]byte, error)

RenderBytes render view with given data to bytes. Return bytes length wrote and any error if raised.

func (*NamedView) RenderError

func (v *NamedView) RenderError(writer http.ResponseWriter, data interface{}, status int) (int, error)

RenderError render view with given data and status code to response. Return bytes length wrote and any error if raised.

type Option

type Option interface {
	ApplyTo(*Renderer) error
}

Option renderer init option interface.

type OptionCommon

type OptionCommon struct {
	//Engine render engine
	Engine Engine
	//ViewRoot root path of view
	ViewRoot string
}

OptionCommon Common renderer option

func NewOptionCommon

func NewOptionCommon() *OptionCommon

NewOptionCommon create new renderer option

func (*OptionCommon) ApplyTo

func (o *OptionCommon) ApplyTo(r *Renderer) error

ApplyTo apply option to renderer

type Renderer

type Renderer struct {

	//ViewFiles view file info map.
	Views map[string]*ViewConfig
	//Views complied views map.
	CompiledViews map[string]CompiledView
	//Developing Developing mode.If set to true,All views will not be cached.
	DevelopmentMode bool
	// contains filtered or unexported fields
}

Renderer renderer main struct

func New

func New() *Renderer

New create new renderer

func (*Renderer) Engine

func (r *Renderer) Engine() Engine

Engine return engine of renderer.

func (*Renderer) Error

func (r *Renderer) Error(w http.ResponseWriter, status int) (int, error)

Error write a http error to response Return bytes length wrote. Panic if any error raised.

func (*Renderer) Execute

func (r *Renderer) Execute(viewname string, data interface{}) ([]byte, error)

Execute execute view by name with given render data. Return render result as []byte and any error if raised.

func (*Renderer) GetView

func (r *Renderer) GetView(ViewName string) *NamedView

GetView get view by name.

func (*Renderer) HTMLFile

func (r *Renderer) HTMLFile(w http.ResponseWriter, path string, status int) (int, error)

HTMLFile write content of given file to response as html. Return bytes length wrote and any error if raised.

func (*Renderer) Init

func (r *Renderer) Init(option Option) error

Init init renderer with option.

func (*Renderer) InitViews

func (r *Renderer) InitViews(option ViewsOption) (map[string]*NamedView, error)

InitViews init renderer views with views option. Return inited views and any error if raised.

func (*Renderer) JSON

func (r *Renderer) JSON(w http.ResponseWriter, data interface{}, status int) (int, error)

JSON marshal data as json and write to response Return bytes length wrote and any error if raised.

func (*Renderer) MustError

func (r *Renderer) MustError(w http.ResponseWriter, status int) int

MustError write a http error to response Return bytes length wrote. Panic if any error raised.

func (*Renderer) MustHTMLFile

func (r *Renderer) MustHTMLFile(w http.ResponseWriter, path string, status int) int

MustHTMLFile write content of given file to response as html. Return bytes length wrote. Panic if any error raised.

func (*Renderer) MustInitViews

func (r *Renderer) MustInitViews(option ViewsOption) map[string]*NamedView

MustInitViews init renderer views with views option. Return inited views. Panic if any error raised.

func (*Renderer) MustJSON

func (r *Renderer) MustJSON(w http.ResponseWriter, data interface{}, status int) int

MustJSON marshal data as json and write to response Return bytes length wrote. Panic if any error raised.

func (*Renderer) MustWriteHTML

func (r *Renderer) MustWriteHTML(w http.ResponseWriter, data []byte, status int) int

MustWriteHTML write html data to response. Return bytes length wrote. Panic if any error raised.

func (*Renderer) MustWriteJSON

func (r *Renderer) MustWriteJSON(w http.ResponseWriter, data []byte, status int) int

MustWriteJSON write json data to response. Return bytes length wrote. Panic if any error raised.

func (*Renderer) NewView

func (r *Renderer) NewView(ViewName string, config *ViewConfig) *NamedView

NewView create new view by name with given view files.

func (*Renderer) WriteHTML

func (r *Renderer) WriteHTML(w http.ResponseWriter, data []byte, status int) (int, error)

WriteHTML write html data to response. Return bytes length wrote and any error if raised.

func (*Renderer) WriteJSON

func (r *Renderer) WriteJSON(w http.ResponseWriter, data []byte, status int) (int, error)

WriteJSON write json data to response. Return bytes length wrote and any error if raised.

type ViewConfig

type ViewConfig struct {
	DevelopmentMode bool
	Files           []string
}

ViewConfig view config struct.

func NewViewConfig

func NewViewConfig(files ...string) *ViewConfig

NewViewConfig create new view config with given view files

type ViewError

type ViewError struct {
	ViewName string
	// contains filtered or unexported fields
}

ViewError view error struct.

func NewViewError

func NewViewError(ViewName string, err error) *ViewError

NewViewError create view error by view name and raw error.

func (*ViewError) Error

func (ve *ViewError) Error() string

Error view error message.

type ViewsOption

type ViewsOption interface {
	ApplyTo(*Renderer) (map[string]*NamedView, error)
}

ViewsOption renderer views init option.

type ViewsOptionCommon

type ViewsOptionCommon struct {
	DevelopmentMode bool
	Views           map[string]*ViewConfig
}

ViewsOptionCommon views option with new view configs.

func (ViewsOptionCommon) ApplyTo

func (o ViewsOptionCommon) ApplyTo(r *Renderer) (map[string]*NamedView, error)

ApplyTo init renderer with given json conf.

Directories

Path Synopsis
engines
jet

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL