render

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2019 License: MIT Imports: 9 Imported by: 102

README

Render

Render provides handy controls when rendering templates.

Usage

Initialize Render
import "github.com/qor/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)
}

License

Released under the MIT License.

Documentation

Overview

Package render support to render templates by your control.

Index

Constants

View Source
const DefaultLayout = "application"

DefaultLayout default layout name

View Source
const DefaultViewPath = "app/views"

DefaultViewPath default view path

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	ViewPaths       []string
	DefaultLayout   string
	FuncMapMaker    func(render *Render, request *http.Request, writer http.ResponseWriter) template.FuncMap
	AssetFileSystem assetfs.Interface
}

Config render config

type Render

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

Render the render struct.

func New

func New(config *Config, viewPaths ...string) *Render

New initalize the render struct.

func (*Render) Asset

func (render *Render) Asset(name string) ([]byte, error)

Asset get content from AssetFS by name

func (*Render) Execute

func (render *Render) Execute(name string, context interface{}, request *http.Request, writer http.ResponseWriter) error

Execute render template with default "application" layout.

func (*Render) Funcs

func (render *Render) Funcs(funcMap template.FuncMap) *Template

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

func (*Render) Layout

func (render *Render) Layout(name string) *Template

Layout set layout for template.

func (*Render) PrependViewPath

func (render *Render) PrependViewPath(paths ...string)

PrependViewPath prepend view path

func (*Render) RegisterFuncMap

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

RegisterFuncMap register FuncMap for render.

func (*Render) RegisterViewPath

func (render *Render) RegisterViewPath(paths ...string)

RegisterViewPath register view path

func (*Render) SetAssetFS

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

SetAssetFS set asset fs for render

type Template

type Template struct {
	// contains filtered or unexported fields
}

Template template struct

func (*Template) Execute

func (tmpl *Template) Execute(templateName string, obj interface{}, req *http.Request, w http.ResponseWriter) error

Execute execute tmpl

func (*Template) Funcs

func (tmpl *Template) Funcs(funcMap template.FuncMap) *Template

Funcs register Funcs for tmpl

func (*Template) Render

func (tmpl *Template) Render(templateName string, obj interface{}, request *http.Request, writer http.ResponseWriter) (template.HTML, error)

Render render tmpl

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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