template

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2023 License: MIT Imports: 14 Imported by: 6

README

template

GitHub Workflow Status Codecov GoDoc Sourcegraph

Package template is a middleware that provides Go template rendering for Flamego.

Installation

The minimum requirement of Go is 1.18.

go get github.com/flamego/template

Getting started

<!-- templates/home.tmpl -->
<p>
  Hello, <b>{{.Name}}</b>!
</p>
package main

import (
	"net/http"

	"github.com/flamego/flamego"
	"github.com/flamego/template"
)

func main() {
	f := flamego.Classic()
	f.Use(template.Templater())
	f.Get("/", func(t template.Template, data template.Data) {
		data["Name"] = "Joe"
		t.HTML(http.StatusOK, "home")
	})
	f.Run()
}

Getting help

License

This project is under the MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Templater

func Templater(opts ...Options) flamego.Handler

Templater returns a middleware handler that injects template.Templater and template.Data into the request context, which are used for rendering templates to the ResponseWriter.

When running with flamego.EnvTypeDev, if either Directory or AppendDirectories is specified, templates will be recompiled upon every request.

Types

type Data

type Data map[string]interface{}

Data is used as the root object for rendering a template.

type Delims

type Delims struct {
	// Left is the left delimiter. Default is "{{".
	Left string
	// Right is the right delimiter. Default is "}}".
	Right string
}

Delims is a pair of Left and Right delimiters for rendering HTML templates.

type File

type File interface {
	// Name returns the name of the file, stripping its extension. It should return
	// "home" not "home.tmpl".
	Name() string
	// Data returns the file content.
	Data() ([]byte, error)
	// Ext returns the file extension, carrying the dot ("."). It should return
	// ".tmpl" not "tmpl".
	Ext() string
}

File is a template file that contains name, data and its extension.

type FileSystem

type FileSystem interface {
	// Files returns the the list of template files.
	Files() []File
}

FileSystem is a template file system consists a list of template files.

func EmbedFS

func EmbedFS(efs embed.FS, dir string, allowedExtensions []string) (FileSystem, error)

EmbedFS wraps the given embed.FS into a FileSystem.

type Options

type Options struct {
	// FileSystem is the interface for supporting any implementation of the
	// FileSystem.
	FileSystem FileSystem
	// Directory is the primary directory to load templates. This value is ignored
	// when FileSystem is set. Default is "templates".
	Directory string
	// AppendDirectories is a list of additional directories to load templates for
	// overwriting templates that are loaded from FileSystem or Directory.
	AppendDirectories []string
	// Extensions is a list of extensions to be used for template files. Default is
	// `[".tmpl", ".html"]`.
	Extensions []string
	// FuncMaps is a list of `template.FuncMap` to be applied for rendering
	// templates.
	FuncMaps []gotemplate.FuncMap
	// Delims is the pair of left and right delimiters for rendering templates.
	Delims Delims
	// ContentType specifies the value of "Content-Type". Default is "text/html".
	ContentType string
}

Options contains options for the template.Templater middleware.

type Template

type Template interface {
	// HTML renders the named template with the given status.
	HTML(status int, name string)
}

Template is a Go template rendering engine.

Jump to

Keyboard shortcuts

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