view

package
v0.0.0-...-a11cca7 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2024 License: AGPL-3.0, MIT Imports: 12 Imported by: 0

README

view

Package view provides template registration, rendering, and helper functions for golang views

Usage

Load templates on app startup:

	err := view.LoadTemplates()
	if err != nil {
		server.Fatalf("Error reading templates %s", err)
	}

Render a template

    // Set up the view
	view := view.New(context)
    // Add a key to the view
    view.AddKey("page", page)
    // Optionally set template, layout or other attributes
    view.Template("src/pages/views/home.html.got")
    // Render the view
    return view.Render()

Public subpackages:

  • helpers - utilities for handling files

Documentation

Overview

Package view provides methods for rendering templates, and helper functions for golang views

Index

Constants

This section is empty.

Variables

View Source
var AuthenticityContext = &ctxKey{authenticityKey}

AuthenticityContext is used as a key to save request authenticity tokens

View Source
var Helpers parser.FuncMap

Helpers is a list of functions available in templates

View Source
var LanguageContext = &ctxKey{languageKey}

LanguageContext is used as a key to save request lang

View Source
var NonceContext = &ctxKey{nonceKey}

NonceContext is used as a key to save request nonce

View Source
var Production bool

Production is true if this server is running in production mode

Functions

func DefaultHelpers

func DefaultHelpers() parser.FuncMap

DefaultHelpers returns a default set of helpers for the app, which can then be extended/replaced. Helper functions may not be changed after LoadTemplates is called, as reloading is required if they change.

func LoadTemplates

func LoadTemplates() error

LoadTemplates loads our templates from ./src, and assigns them to the package variable Templates This function is deprecated and will be removed, use LoadTemplatesAtPaths instead

func LoadTemplatesAtPaths

func LoadTemplatesAtPaths(paths []string, helpers parser.FuncMap) error

LoadTemplatesAtPaths loads our templates given the paths provided

func PrintTemplates

func PrintTemplates()

PrintTemplates prints out our list of templates for debug

func ReloadTemplates

func ReloadTemplates() error

ReloadTemplates reloads the templates for our scanner

Types

type RenderContext

type RenderContext interface {
	Path() string
	RenderContext() map[string]interface{}
	Writer() http.ResponseWriter
}

RenderContext is the type passed in to New, which helps construct the rendering view Alternatively, you can use NewWithPath, which doesn't require a RenderContext

type Renderer

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

Renderer is a view which is set up on each request and renders the response to its writer

func New

func New(c RenderContext) *Renderer

New creates a new Renderer

func NewRenderer

func NewRenderer(w http.ResponseWriter, r *http.Request) *Renderer

NewRenderer returns a new renderer for this request.

func NewWithPath

func NewWithPath(p string, w http.ResponseWriter) *Renderer

NewWithPath creates a new Renderer with a path and an http.ResponseWriter

func (*Renderer) AddKey

func (r *Renderer) AddKey(key string, value interface{}) *Renderer

AddKey adds a key/value pair to context

func (*Renderer) CacheKey

func (r *Renderer) CacheKey(key string)

CacheKey sets the Cache-Control and Etag headers on the response using the CacheKey() from the Cacher passed in

func (*Renderer) Context

func (r *Renderer) Context(c map[string]interface{}) *Renderer

Context sets the entire context for rendering

func (*Renderer) Format

func (r *Renderer) Format(format string) *Renderer

Format sets the format used, e.g. text/html,

func (*Renderer) HTML

func (r *Renderer) HTML(content string) *Renderer

HTML sets the view content as html (use with caution)

func (*Renderer) Header

func (r *Renderer) Header(k, v string) *Renderer

Header sets a header on the Renderer's Writer (if set)

func (*Renderer) Layout

func (r *Renderer) Layout(layout string) *Renderer

Layout sets the layout used

func (*Renderer) Path

func (r *Renderer) Path(p string) *Renderer

Path sets the request path on the renderer (used for choosing a default template)

func (*Renderer) Render

func (r *Renderer) Render() error

Render our template into layout using our context and write out to writer

func (*Renderer) RenderToString

func (r *Renderer) RenderToString() (string, error)

RenderToString renders our template into layout using our context and return a string

func (*Renderer) RenderToStringWithLayout

func (r *Renderer) RenderToStringWithLayout() (string, error)

RenderToStringWithLayout renders our template into layout using our context and return a string

func (*Renderer) Response

func (r *Renderer) Response() error

Response renders our template into layout using our context and write out to writer Response is an alternative name for Render so that we can call render.Response(), it may replace it eventually.

func (*Renderer) SendFile

func (r *Renderer) SendFile(p string) error

SendFile writes the file at the given path out to our writer it assumes the appropriate headers have been set first (Content-Type, Content-Disposition) e.g.:

	view.Header("Content-type", "application/pdf")
	view.Header("Content-Disposition", "attachment; filename='myfile.pdf'")
 view.SendFile(mypath)

func (*Renderer) Status

func (r *Renderer) Status(status int) *Renderer

Status sets the Renderer status

func (*Renderer) Template

func (r *Renderer) Template(template string) *Renderer

Template sets the template used

func (*Renderer) Text

func (r *Renderer) Text(content string) *Renderer

Text sets the view content as text

Directories

Path Synopsis
Package parser defines an interface for parsers which the base template conforms to
Package parser defines an interface for parsers which the base template conforms to
Package translation provides a simple in-memory translation service - it may soon be renamed translate
Package translation provides a simple in-memory translation service - it may soon be renamed translate

Jump to

Keyboard shortcuts

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