views

package
v1.7.4 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: GPL-2.0 Imports: 23 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ErrMissingArg errs.Error = "missing url argument or argument is empty"
)

Variables

This section is empty.

Functions

func Cache added in v1.6.6

func Cache(view http.Handler, duration time.Duration, cacheBackends ...string) http.Handler
TODO: FIX REQUEST CACHE IMPLEMENTATION

Cache caches the response of a view for a given duration.

func GetViewContext added in v1.7.2

func GetViewContext(req *http.Request, view any) (context ctx.Context, err error)

func Invoke

func Invoke(view View, w http.ResponseWriter, req *http.Request, allowedMethods ...string) error

Invoke invokes the view and appropriately handles the request.

This view is a generic view that can be used to render templates.

It is moved into a separate function to better handle views and their composition.

Example:

type MyView struct {
	TemplateView // Some sort of base view with a get context method.
	// Fields here...
}

func (v *MyView) GetContext(req *http.Request) (ctx.Context, error) {
	// Get the context here...
}

In regular GO, the above TemplateView would not be able to get the context of the 'MyView' struct.

This means the overridden GetContext method would remain completely invisible and thus go unused.

This prevents a whole lot of flexibility in the design of the views.

This function is a workaround to allow for a more class-based approach.

It can handle the following types of views:

  1. View - A generic view that can handle any type of request.
  2. MethodsView - A view that declares acceptable methods.
  3. ContextGetter - A view that can initiate a context for the request.
  4. TemplateGetter - A view that can get the template path of the template to render.
  5. TemplateView - A view that can render a template from a template path with a context.
  6. ErrorHandler - A view that can handle errors.
  7. Renderer - A view that can render directly to the response writer with a context.
  8. TemplateKeyer - A view that can get the base key for the template. This is useful for rendering a sub-template with a base template.
  9. Checker - A view that can check the request before serving it. This is useful for checking if the request is valid before serving it.

func MethodServe added in v1.7.2

func MethodServe(w http.ResponseWriter, req *http.Request, view any) bool

func MethodServeContext added in v1.7.2

func MethodServeContext(w http.ResponseWriter, req *http.Request, view any, context ctx.Context) bool

func Serve

func Serve(view View) http.Handler

Serve serves a view.

This function is a generic view handler that can be used to serve any type of view which the `Invoke` function can handle.

func TryServeTemplateView added in v1.7.2

func TryServeTemplateView(w http.ResponseWriter, req *http.Request, views []View, context ctx.Context) error

Types

type BaseView

type BaseView struct {
	AllowedMethods  []string
	BaseTemplateKey string
	TemplateName    string
	GetContextFn    func(req *http.Request) (ctx.Context, error)
}

func (*BaseView) GetContext

func (v *BaseView) GetContext(req *http.Request) (ctx.Context, error)

func (*BaseView) GetTemplate

func (v *BaseView) GetTemplate(req *http.Request) string

func (*BaseView) Methods

func (v *BaseView) Methods() []string

func (*BaseView) Render

func (v *BaseView) Render(w http.ResponseWriter, req *http.Request, templateName string, context ctx.Context) error

func (*BaseView) ServeXXX

func (v *BaseView) ServeXXX(w http.ResponseWriter, req *http.Request)

Placeholder method, will never get called.

type BindableView added in v1.7.2

type BindableView interface {
	View

	// Bind binds the view to the request and response writer.
	// It returns a new view, specific for said request.
	Bind(w http.ResponseWriter, req *http.Request) (View, error)
}

type CachedResponse added in v1.6.6

type CachedResponse struct {
	ResponseStatusCode int         `json:"status_code"`
	ResponseHeader     http.Header `json:"header"`
	ResponseBody       byteArray   `json:"body"`
}

type Checker

type Checker interface {
	View

	// Check checks the request before serving it.
	// Useful for checking if the request is valid before serving it.
	// Like checking permissions, etc...
	Check(w http.ResponseWriter, req *http.Request) error

	// Fail is a helper function to fail the request.
	// This can be used to redirect, etc.
	Fail(w http.ResponseWriter, req *http.Request, err error)
}

type ContextGetter

type ContextGetter interface {
	View
	GetContext(req *http.Request) (ctx.Context, error)
}

type ContextHandlerFunc added in v1.7.2

type ContextHandlerFunc = func(w http.ResponseWriter, req *http.Request, ctx ctx.Context) error

type ControlledView

type ControlledView interface {
	View
	TakeControl(w http.ResponseWriter, req *http.Request, v View)
}

type DetailView

type DetailView[T any] struct {
	BaseView
	ContextName string
	URLArgName  string
	GetObjectFn func(req *http.Request, urlArg string) (T, error)
}

func (*DetailView[T]) GetContext

func (d *DetailView[T]) GetContext(req *http.Request) (ctx.Context, error)

func (*DetailView[T]) GetObject

func (d *DetailView[T]) GetObject(req *http.Request, urlArg string) (v T, err error)

func (*DetailView[T]) GetURLArg

func (d *DetailView[T]) GetURLArg(req *http.Request) (string, error)

func (*DetailView[T]) Setup

type ErrorFunc

type ErrorFunc func(w http.ResponseWriter, req *http.Request, err error, code int)

type ErrorHandler

type ErrorHandler interface {
	// HandleError handles the error.
	HandleError(w http.ResponseWriter, req *http.Request, err error, code int)
}

type FormView

type FormView[T forms.Form] struct {
	BaseView
	GetFormFn        func(req *http.Request) T
	GetInitialFn     func(req *http.Request) map[string]interface{}
	ValidFn          func(req *http.Request, form T) error
	InvalidFn        func(req *http.Request, form T) error
	SuccessFn        func(w http.ResponseWriter, req *http.Request, form T)
	CheckPermissions func(w http.ResponseWriter, req *http.Request) error
	FailsPermissions func(w http.ResponseWriter, req *http.Request, err error)
}

func (*FormView[T]) Check

func (v *FormView[T]) Check(w http.ResponseWriter, req *http.Request) error

func (*FormView[T]) Fail

func (v *FormView[T]) Fail(w http.ResponseWriter, req *http.Request, err error)

func (*FormView[T]) FormFromCtx

func (v *FormView[T]) FormFromCtx(context ctx.Context) (t T)

func (*FormView[T]) GetContext

func (v *FormView[T]) GetContext(req *http.Request) (ctx.Context, error)

func (*FormView[T]) GetForm

func (v *FormView[T]) GetForm(req *http.Request) T

func (*FormView[T]) Render

func (v *FormView[T]) Render(w http.ResponseWriter, req *http.Request, templateName string, context ctx.Context) error

type HttpContextHandler added in v1.7.2

type HttpContextHandler interface {
	ServeHTTP(http.ResponseWriter, *http.Request, ctx.Context)
}

type MethodsView

type MethodsView interface {
	View
	Methods() []string
}

type Renderer

type Renderer interface {
	View
	ContextGetter

	// Render renders the template with the given context.
	Render(w http.ResponseWriter, req *http.Request, context ctx.Context) error
}

type SetupView

type SetupView interface {
	View
	Setup(w http.ResponseWriter, req *http.Request) (http.ResponseWriter, *http.Request)
}

type TemplateGetter

type TemplateGetter interface {
	// GetTemplate returns the template that will be rendered.
	GetTemplate(req *http.Request) string
}

type TemplateKeyer

type TemplateKeyer interface {
	// GetBaseKey returns the base key for the template.
	GetBaseKey() string
}

type TemplateRenderer added in v1.7.2

type TemplateRenderer interface {
	// Render renders the template with the given context.
	Render(w http.ResponseWriter, req *http.Request, templateName string, context ctx.Context) error
}

type TemplateView

type TemplateView interface {
	View
	ContextGetter
	TemplateGetter
	TemplateRenderer
}

type View

type View interface {
	// ServeXXX is a method that will never get called.
	// It is a placeholder for the actual method that will be called.
	// The actual method will be determined by the type of the request.
	// For example, if the request is a GET request, then ServeGET will be called.
	// If the request is a POST request, then ServePOST will be called.
	// etc...
	ServeXXX(w http.ResponseWriter, req *http.Request)
}

type ViewMixins added in v1.7.2

type ViewMixins interface {
	View
	mixins.Definer[View]
}

Directories

Path Synopsis
templ: version: v0.3.1020
templ: version: v0.3.1020

Jump to

Keyboard shortcuts

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