render

package module
v0.0.0-...-00b5821 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2020 License: MIT Imports: 20 Imported by: 5

README

Render

Render provides handy controls when rendering templates.

Usage

Initialize Render
import "github.com/ecletus/render"

func main() {
  Render := render.New(&render.Config{
    ViewPaths:     []string{"app/new_view_path"},
    DefaultLayout: "application", // default value is application
    FuncMapMaker:  func(*Render, *http.Request, http.ResponseWriter) template.FuncMap {
      // genereate FuncMap that could be used when render template based on request info
    },
  })
}

Next, invoke Execute function to render your template...

Render.Execute("index", context, request, writer)

The Execute function accepts 4 parameters:

  1. The template name. In this example Render will look up template index.tmpl from view paths. the default view path is {current_repo_path}/app/views, and you could register more view paths.
  2. The context you can use in the template, it is an interface{}, you could use that in views. for example, if you pass context["CurrentUserName"] = "Memememe" as the context. In the template, you can call {% raw %}{{.CurrentUserName}}{% endraw %} to get the value "Memememe".
  3. http.Request of Go.
  4. http.ResponseWriter of Go.
Understanding yield

yield is a func that could be used in layout views, it will render current specified template. For above example, think yield as a placeholder, and it will replaced with template index.tmpl's content.

<!-- app/views/layout/application.tmpl -->
<html>
  <head>
  </head>
  <body>
    {{yield}}
  </body>
</html>
Specify Layout

The default layout is {current_repo_path}/app/views/layouts/application.tmpl. If you want use another layout like new_layout, you can pass it as a parameter to Layout function.

Render.Layout("new_layout").Execute("index", context, request, writer)

Render will find the layout at {current_repo_path}/app/views/layouts/new_layout.tmpl.

Render with helper functions

Sometimes you may want to have some helper functions in your template. Render supports passing helper functions by Funcs function.

Render.Funcs(funcsMap).Execute("index", obj, request, writer)

The funcsMap is based on html/template.FuncMap. So with

funcMap := template.FuncMap{
  "Greet": func(name string) string { return "Hello " + name },
}

You can call this in the template

{{Greet "Memememe" }}

The output is Hello Memememe.

Use with Responder

Put the Render inside Responder handle function like this.

func handler(writer http.ResponseWriter, request *http.Request) {
  responder.With("html", func() {
    Render.Execute("demo/index", viewContext, *http.Request, http.ResponseWriter)
  }).With([]string{"json", "xml"}, func() {
    writer.Write([]byte("this is a json or xml request"))
  }).Respond(request)
})
Use with Bindatafs
$ bindatafs --exit-after-compile=false config/views

func main() {
	Render := render.New()
	Render.SetAssetFS(views.AssetFS)

	Render.Execute("index", context, request, writer)
}

Documentation

Overview

Package render support to render templates by your control.

Index

Constants

View Source
const DefaultLayout = "application"

DefaultLayout default layout name

Variables

This section is empty.

Functions

func AddFormHandler

func AddFormHandler(c context.Context, handlers ...*FormHandler) context.Context

func AddScriptHandler

func AddScriptHandler(c context.Context, handlers ...*ScriptHandler) context.Context

func AddStyleHandler

func AddStyleHandler(c context.Context, handlers ...*StyleHandler) context.Context

func Context

func Context(s *template.State, defaul ...*core.Context) *core.Context

func DefaultLocale

func DefaultLocale() string

Types

type Config

type Config struct {
	PageHandlers
	DefaultLayout string
	FuncMapMaker  FuncMapMaker
	AssetFS       assetfs.Interface
	DebugFiles    bool
	DefaultLocale string
	Abouter       about.SiteAbouter
}

Config render config

type FormHandler

type FormHandler struct {
	Name    string
	Handler func(state *FormState, ctx *core.Context) (err error)
}

type FormHandlers

type FormHandlers []*FormHandler

func GetFormHandlers

func GetFormHandlers(c context.Context) (r FormHandlers)

func (*FormHandlers) Append

func (this *FormHandlers) Append(handlers ...*FormHandler) FormHandlers

func (FormHandlers) AppendCopy

func (this FormHandlers) AppendCopy(handlers ...*FormHandler) FormHandlers

type FormState

type FormState struct {
	Name string
	Body string
}

type FuncMapMaker

type FuncMapMaker func(values *template.FuncValues, render *Render, context *core.Context) error

type PageHandler

type PageHandler interface {
	GetFormHandlers() *FormHandlers
	GetScriptHandlers() *ScriptHandlers
	GetStyleHandlers() *StyleHandlers
}

type PageHandlers

type PageHandlers struct {
	FormHandlers   FormHandlers
	ScriptHandlers ScriptHandlers
	StyleHandlers  StyleHandlers
}

func (*PageHandlers) GetFormHandlers

func (this *PageHandlers) GetFormHandlers() *FormHandlers

func (*PageHandlers) GetScriptHandlers

func (this *PageHandlers) GetScriptHandlers() *ScriptHandlers

func (*PageHandlers) GetStyleHandlers

func (this *PageHandlers) GetStyleHandlers() *StyleHandlers

type Render

type Render struct {
	*Config
	// contains filtered or unexported fields
}

Render the render struct.

func New

func New(config *Config) *Render

New initalize the render struct.

func (*Render) Asset

func (this *Render) Asset(name string) (asset assetfs.AssetInterface, err error)

Asset get content from AssetFS by name

func (*Render) Execute

func (this *Render) Execute(name string, data interface{}, context *core.Context) error

Execute render template with default "application" layout.

func (*Render) Funcs

func (this *Render) Funcs() template.FuncValues

Funcs set helper functions for template with default "application" layout.

func (*Render) FuncsPtr

func (this *Render) FuncsPtr() *template.FuncValues

FuncsPtr set helper functions for template with default "application" layout.

func (*Render) Layout

func (this *Render) Layout(name string) (t *Template)

Layout set layout for template.

func (*Render) RegisterFuncMap

func (this *Render) RegisterFuncMap(name string, fc interface{})

RegisterFuncMap register FuncMap for render.

func (*Render) RegisterFuncMapMaker

func (this *Render) RegisterFuncMapMaker(name string, fm FuncMapMaker)

RegisterFuncMapMaker register FuncMap for render.

func (*Render) SetAssetFS

func (this *Render) SetAssetFS(assetFS assetfs.Interface)

SetAssetFS set asset fs for render

func (*Render) Template

func (this *Render) Template() *Template

type ScriptHandler

type ScriptHandler struct {
	Name    string
	Handler func(state *template.State, ctx *core.Context, w io.Writer) (err error)
}

type ScriptHandlers

type ScriptHandlers []*ScriptHandler

func GetScriptHandlers

func GetScriptHandlers(c context.Context) (r ScriptHandlers)

func (*ScriptHandlers) Append

func (this *ScriptHandlers) Append(handlers ...*ScriptHandler) ScriptHandlers

func (ScriptHandlers) AppendCopy

func (this ScriptHandlers) AppendCopy(handlers ...*ScriptHandler) ScriptHandlers

type StyleHandler

type StyleHandler struct {
	Name    string
	Handler func(state *template.State, ctx *core.Context, w io.Writer) (err error)
}

type StyleHandlers

type StyleHandlers []*StyleHandler

func GetStyleHandlers

func GetStyleHandlers(c context.Context) (r StyleHandlers)

func (*StyleHandlers) Append

func (this *StyleHandlers) Append(handlers ...*StyleHandler) StyleHandlers

func (StyleHandlers) AppendCopy

func (this StyleHandlers) AppendCopy(handlers ...*StyleHandler) StyleHandlers

type Template

type Template struct {
	render.Template

	DebugFiles bool
	// contains filtered or unexported fields
}

Template template struct

func NewTemplate

func NewTemplate(render *Render) (t *Template)

func (*Template) Execute

func (this *Template) Execute(templateName string, obj interface{}, context *core.Context) (err error)

Execute execute tmpl

func (Template) Render

func (this Template) Render(state *template.State, templateName string, obj interface{}, ctx *core.Context, lang ...string) (s template.HTML, err error)

Render render tmpl

func (Template) RenderW

func (this Template) RenderW(state *template.State, w io.Writer, templateName string, obj interface{}, ctx *core.Context, lang ...string) (err error)

Render render tmpl

func (Template) SetFuncValues

func (this Template) SetFuncValues(fv ...template.FuncValues) *Template

func (Template) SetFuncs

func (this Template) SetFuncs(fv ...template.FuncMap) *Template

func (Template) SetLayout

func (this Template) SetLayout(layout string) *Template

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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