templates

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2020 License: MIT Imports: 10 Imported by: 0

README

gin-templates

GoDoc Build Status Coverage Status Go Report Card Issues

  • Loads a tree of HTML templates into Gin
  • Allows many templates from a directory tree

Installation

go get -u github.com/rickb777/gin-templates

Usage

Given a directory templates containing your templates and given that their names all end in ".html", use this setup

engine := gin.New()
engine.FuncMap = (... as needed ...)
engine.HTMLRender = gin_templates.LoadTemplates("templates", ".html", engine.FuncMap)

This scans all ".html" files in templates and its subdirectories. So, for example, if files templates/foo/home.html and templates/foo/bar/baz.html exist, when loaded they will have the names foo/home.html and foo/bar/baz.html respectively.

As usual, your handlers will use Context.HTML(), e.g

c.HTML(200, "foo/home.html", dataModel)

will execute the template with the path foo/home.html.

It keeps the same DebugMode and ReleaseMode behaviour as the core Gin rendering. So in DebugMode, templates are hot-reloaded every time they are used.

Rationale

Go templates work very well and are widely used. There is, however, an unfortunate feature of the main template.ParseFiles and template.ParseGlob functions only work well for small numbers of templates. The name they store of each template is based on the filename ignoring its path; therefore any name collisions cause templates to be unusable. For a larger number of templates, this problem becomes increasingly likely.

So gin-templates provides a way to load templates without being affected by this issue. The template files can be organised in directories as needed and their names will reflect their path relative to the root directory of the templates.

Status

This library is in early development.

Credits

This package was inspired by multitemplate.

Documentation

Index

Constants

View Source
const (
	TextHtml         = "text/html"
	ApplicationXhtml = "application/xhtml+xml"
)

Variables

View Source
var ContentType = TextHtml

ContentType is the value returned when serving HTML files. This defaults to TextHtml, but set it to ApplicationXhtml if more appropriate.

Functions

This section is empty.

Types

type HTMLProcessor added in v0.3.0

type HTMLProcessor interface {
	render.HTMLRender
	ResponseProcessor
}

func LoadTemplates

func LoadTemplates(dir, suffix string, funcMap template.FuncMap) HTMLProcessor

LoadTemplates finds all the templates in the directory dir and its subdirectories that have names ending with the given suffix. The function map can be nil if not required.

type ResponseProcessor added in v0.3.0

type ResponseProcessor interface {
	// CanProcess is the predicate that determines whether this processor
	// will handle a given request.
	CanProcess(mediaRange string, lang string) bool
	// ContentType returns the content type for this response.
	ContentType() string
	// Process renders the data model to the response writer, without setting any headers.
	// If the processor encounters an error, it should panic.
	Process(w http.ResponseWriter, template string, dataModel interface{}) error
}

ResponseProcessor interface creates the contract for custom content negotiation. This matches processor.ResponseProcessor (github.com/rickb777/negotiator) without introducing an explicit dependency.

Jump to

Keyboard shortcuts

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