templates

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2020 License: BSD-3-Clause Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidPath is returned from the Load function and a FileReader when
	// the path argument is not valid.
	ErrInvalidPath = errors.New("scriggo: invalid path")

	// ErrNotExist is returned from the Load function when the path does not
	// exist.
	ErrNotExist = errors.New("scriggo: path does not exist")

	// ErrReadTooLarge is returned from the Load function when a limit is
	// exceeded reading a path.
	ErrReadTooLarge = errors.New("scriggo: read too large")
)

Functions

func ValidDirReaderPath

func ValidDirReaderPath(path string) bool

ValidDirReaderPath reports whether path is valid as name for DirReader and DirLimitedReader.

Types

type CSS

type CSS string

CSS implements the CSSStringer interface.

func (CSS) CSS

func (css CSS) CSS() string

type CSSEnvStringer

type CSSEnvStringer interface {
	CSS(runtime.Env) string
}

CSSEnvStringer is like CSSStringer where the CSS method takes a runtime.Env parameter.

type CSSStringer

type CSSStringer interface {
	CSS() string
}

CSSStringer is implemented by values that are not escaped in CSS context.

type CompilerError

type CompilerError interface {
	error
	Position() ast.Position
	Path() string
	Message() string
}

CompilerError represents an error returned by the compiler.

type Declarations

type Declarations map[string]interface{}

Declarations.

type DirLimitedReader

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

DirLimitedReader implements a FileReader that reads a source from files in a directory limiting the maximum file size and the total bytes read from all reads.

Use DirLimitedReader, instead of DirReader, when you do not have control of file sizes. As a Parser reads a file with a specific context only once, DirLimitedReader can be passed to a Parser to prevent it from allocating too much memory.

func NewDirLimitedReader

func NewDirLimitedReader(dir string, maxFile, maxTotal int) *DirLimitedReader

NewDirLimitedReader returns a DirLimitedReader that reads the file from directory dir limiting the file size to maxFile bytes and the total bytes read from all files to maxTotal. It panics if maxFile or maxTotal are negative.

func (*DirLimitedReader) ReadFile

func (dr *DirLimitedReader) ReadFile(name string) ([]byte, error)

ReadFile implements the ReadFile method of FileReader. If a limit is exceeded it returns the error ErrReadTooLarge.

type DirReader

type DirReader string

DirReader implements a FileReader that reads the files in a directory.

To limit the size of read files, use DirLimitedReader instead.

func (DirReader) ReadFile

func (dir DirReader) ReadFile(name string) ([]byte, error)

ReadFile implements the ReadFile method of FileReader.

type EnvStringer

type EnvStringer interface {
	String(runtime.Env) string
}

EnvStringer is like fmt.Stringer where the String method takes a runtime.Env parameter.

type FileReader

type FileReader interface {
	ReadFile(name string) ([]byte, error)
}

FileReader is implemented by values that can read files of a template. name, if not absolute, is relative to the root of the template. If the file with the given name does not exist, it returns nil and an os not found error.

type HTML

type HTML string

HTML implements the HTMLStringer interface.

func (HTML) HTML

func (html HTML) HTML() string

type HTMLEnvStringer

type HTMLEnvStringer interface {
	HTML(runtime.Env) string
}

HTMLEnvStringer is like HTMLStringer where the HTML method takes a runtime.Env parameter.

type HTMLStringer

type HTMLStringer interface {
	HTML() string
}

HTMLStringer is implemented by values that are not escaped in HTML context.

type JSON added in v0.16.0

type JSON string

JSON implements the JSONStringer interface.

func (JSON) JSON added in v0.16.0

func (json JSON) JSON() string

type JSONEnvStringer added in v0.16.0

type JSONEnvStringer interface {
	JSON(runtime.Env) string
}

JSONEnvStringer is like JSONStringer where the JSON method takes a runtime.Env parameter.

type JSONStringer added in v0.16.0

type JSONStringer interface {
	JSON() string
}

JSONStringer is implemented by values that are not escaped in JSON context.

type JavaScript

type JavaScript string

JavaScript implements the JavaScriptStringer interface.

func (JavaScript) JavaScript

func (js JavaScript) JavaScript() string

type JavaScriptEnvStringer

type JavaScriptEnvStringer interface {
	JavaScript(runtime.Env) string
}

JavaScriptEnvStringer is like JavaScriptStringer where the JavaScript method takes a runtime.Env parameter.

type JavaScriptStringer

type JavaScriptStringer interface {
	JavaScript() string
}

JavaScriptStringer is implemented by values that are not escaped in JavaScript context.

type Language

type Language int

A Language represents a source language.

const (
	LanguageText Language = iota
	LanguageHTML
	LanguageCSS
	LanguageJavaScript
	LanguageJSON
)

func (Language) String

func (language Language) String() string

type LoadOptions

type LoadOptions struct {
	DisallowGoStmt  bool
	TreeTransformer func(*ast.Tree) error // if not nil transforms tree after parsing.

	// Builtins declares constants, types, variables and functions that are
	// accessible from the code in the template.
	Builtins Declarations

	// Loader is a package loader that makes precompiled packages available in
	// the template through the 'import' statement.
	//
	// Note that an import statement refers to a precompiled package read from
	// the Loader if its path has no extension.
	//
	//     {%  import  "my/package"   %}    Import a precompiled package.
	//     {%  import  "my/file.html  %}    Import a template file.
	//
	Loader scriggo.PackageLoader
}

type MapReader

type MapReader map[string][]byte

MapReader implements a FileReader where sources are read from a map. Map keys are the file names. If a name is present both relative and absolute, the file of the absolute one is returned.

func (MapReader) ReadFile

func (r MapReader) ReadFile(name string) ([]byte, error)

ReadFile implements the ReadFile method of FileReader.

type PrintTypeError

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

func (PrintTypeError) Error

func (err PrintTypeError) Error() string

func (PrintTypeError) RuntimeError

func (err PrintTypeError) RuntimeError()

type RenderOptions

type RenderOptions struct {
	Context   context.Context
	PrintFunc runtime.PrintFunc
}

type Template

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

func Load

func Load(name string, files FileReader, lang Language, options *LoadOptions) (*Template, error)

Load loads a template given its file name. Load calls the method ReadFile of files to read the files of the template.

func (*Template) Disassemble

func (t *Template) Disassemble(w io.Writer) (int64, error)

Disassemble disassembles a template.

func (*Template) MustRender

func (t *Template) MustRender(out io.Writer, vars map[string]interface{}, options *RenderOptions)

MustRender is like Render but panics if the rendering fails.

func (*Template) Render

func (t *Template) Render(out io.Writer, vars map[string]interface{}, options *RenderOptions) error

Render renders the template and write the output to out. vars contains the values of the template builtin variables.

func (*Template) Vars

func (t *Template) Vars() []string

Vars returns the names of the template builtin variables that are used in the template.

Jump to

Keyboard shortcuts

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