templates

package module
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 11 Imported by: 0

README

Templates GO wrapper

Build Status Go Report Card GoDoc Coverage Status

MIT License

Project represents simple wrapper of standart GO templates to make it simpler.

Example

import (
  temp "github.com/gopk/templates/v2"
)

func main() {
  render := temp.NewHTMLFS(fs.Context, "", ".html", isNotDebug())

  mux := http.NewServeMux()
  mux.HandleFunc("/", render.HTTPHandler(func(w http.ResponseWriter, r *http.Request) *temp.HTTPResponse {
    return temp.Response(http.StatusOK, "index", params)
  })
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext added in v2.2.0

func FromContext[T templateIfaceTypes[TT, FM], TT templateTypes, FM ~map[string]any](ctx context.Context) *render[T, TT, FM]

FromContext returns render from context

func HTTPHandler

func HTTPHandler[T templateIfaceTypes[TT, FM], TT templateTypes, FM ~map[string]any](render *render[T, TT, FM], f HTTPResponseHandler) http.HandlerFunc

HTTPHandler reponse wrapper for default http handler

Example: mux := http.NewServeMux() mux.HandleFunc("/", HTTPHandler(render, func(w http.ResponseWriter, r *http.Request) *HTTPResponse){ return Response(http.StatusOK, "index", params) }) mux.HandleFunc("/hello", HTTPHandler(render, getHello))

func New

func New[T templateIfaceTypes[TT, FM], TT templateTypes, FM ~map[string]any](path, postfix string, enabledCache bool) *render[T, TT, FM]

New creates new template render with some option params

@param path - to the directory of templates @param postfix - after file name. You can render template just with name "index", "search" and etc and set the extension of file in the postfix @param enabledCache - option

func NewFS

func NewFS[T templateIfaceTypes[TT, FM], TT templateTypes, FM ~map[string]any](fs fs.FS, path, postfix string, enabledCache bool) *render[T, TT, FM]

NewFS creates new template render with some option params for FS object

@param fs - preinited directory in memory @param path - to the directory of templates inside FS @param postfix - after file name. You can render template just with name "index", "search" and etc and set the extension of file in the postfix @param enabledCache - option

func WithContext added in v2.2.0

func WithContext[T templateIfaceTypes[TT, FM], TT templateTypes, FM ~map[string]any](ctx context.Context, render *render[T, TT, FM]) context.Context

WithContext returns context with render value

Types

type HTMLRender added in v2.2.0

func FromContextHTML added in v2.2.0

func FromContextHTML(ctx context.Context) *HTMLRender

FromContextHTML returns render from context

func NewHTML added in v2.2.0

func NewHTML(path, postfix string, enabledCache bool) *HTMLRender

NewHTML creates new template render with some option params

@param path - to the directory of templates @param postfix - after file name. You can render template just with name "index", "search" and etc and set the extension of file in the postfix @param enabledCache - option

func NewHTMLFS added in v2.2.0

func NewHTMLFS(fs fs.FS, path, postfix string, enabledCache bool) *HTMLRender

NewHTMLFS creates new template render with some option params for FS object

@param fs - preinited directory in memory @param path - to the directory of templates inside FS @param postfix - after file name. You can render template just with name "index", "search" and etc and set the extension of file in the postfix @param enabledCache - option

type HTTPResponse

type HTTPResponse struct {
	Request  *http.Request
	Writer   http.ResponseWriter
	Template string
	Context  Params
	Error    error
	Code     int
}

HTTPResponse represents HTTP response with all necessary information

func Response

func Response(code int, template string, ctx Params) *HTTPResponse

Response returns new response object for HTTPHandler wrapper

func (*HTTPResponse) ExtendParams

func (resp *HTTPResponse) ExtendParams(ctx Params) *HTTPResponse

ExtendParams which will be passed to the renderer

func (*HTTPResponse) SetParams

func (resp *HTTPResponse) SetParams(ctx Params) *HTTPResponse

SetParams which will be passed to the renderer

type HTTPResponseHandler

type HTTPResponseHandler func(w http.ResponseWriter, r *http.Request) *HTTPResponse

HTTPResponseHandler represents default HTTP response handlerer

type Params

type Params map[string]any

Params of the render

type PlainRender added in v2.2.0

func FromContextPlain added in v2.2.0

func FromContextPlain(ctx context.Context) *PlainRender

FromContextPlain returns render from context

func NewPlain added in v2.2.0

func NewPlain(path, postfix string, enabledCache bool) *PlainRender

NewPlain creates new template render with some option params

@param path - to the directory of templates @param postfix - after file name. You can render template just with name "index", "search" and etc and set the extension of file in the postfix @param enabledCache - option

func NewPlainFS added in v2.2.0

func NewPlainFS(fs fs.FS, path, postfix string, enabledCache bool) *PlainRender

NewPlainFS creates new template render with some option params for FS object

@param fs - preinited directory in memory @param path - to the directory of templates inside FS @param postfix - after file name. You can render template just with name "index", "search" and etc and set the extension of file in the postfix @param enabledCache - option

type ResponseHandler

type ResponseHandler func(*HTTPResponse) error

ResponseHandler for the particular HTTP code

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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