templates

package
v0.32.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

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

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

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

Functions

func HTMLEscape added in v0.23.0

func HTMLEscape(s string) string

HTMLEscape escapes s, replacing the characters <, >, &, " and ' and returns the escaped string.

Use HTMLEscape to put a trusted or untrusted string into an HTML element content or in a quoted attribute value. But don't use it with complex attributes like href, src, style, or any of the event handlers like onmouseover.

func ValidDirReaderPath

func ValidDirReaderPath(path string) bool

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

Types

type BuildOptions added in v0.31.0

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

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

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

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 Converter added in v0.32.0

type Converter func(src []byte, out io.Writer) error

Converter is implemented by format converters.

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 Format added in v0.32.0

type Format int

A Format represents a content format.

const (
	FormatText Format = iota
	FormatHTML
	FormatCSS
	FormatJS
	FormatJSON
	FormatMarkdown
)

func (Format) String added in v0.32.0

func (format Format) String() string

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 JS added in v0.30.0

type JS string

JS implements the JSStringer interface.

func (JS) JS added in v0.30.0

func (js JS) JS() string

type JSEnvStringer added in v0.30.0

type JSEnvStringer interface {
	JS(runtime.Env) string
}

JSEnvStringer is like JSStringer where the JS method takes a runtime.Env parameter.

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 JSStringer added in v0.30.0

type JSStringer interface {
	JS() string
}

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

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 Markdown added in v0.31.0

type Markdown string

Markdown implements the MarkdownStringer interface.

func (Markdown) Markdown added in v0.31.0

func (md Markdown) Markdown() string

type MarkdownEnvStringer added in v0.31.0

type MarkdownEnvStringer interface {
	Markdown(runtime.Env) string
}

MarkdownEnvStringer is like MarkdownStringer where the Markdown method takes a runtime.Env parameter.

type MarkdownStringer added in v0.31.0

type MarkdownStringer interface {
	Markdown() string
}

MarkdownStringer is implemented by values that are not escaped in Markdown context.

type RunOptions added in v0.31.0

type RunOptions struct {
	Context   context.Context
	PrintFunc runtime.PrintFunc

	// MarkdownConverter converts a Markdown source code to HTML.
	MarkdownConverter Converter
}

type Template

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

func Build added in v0.31.0

func Build(name string, files FileReader, options *BuildOptions) (*Template, error)

Build builds a template given its file name. Build calls the method ReadFile of files to read the files of the template.

The format of the file depends on the extension of name or, if files has the method 'Format(string) (Format, error)', Build gets the format from this method. If this method returns an error, this error is returned.

func (*Template) Disassemble

func (t *Template) Disassemble(n int) []byte

Disassemble disassembles a template and returns its assembly code.

n determines the maximum length, in runes, of a disassembled text:

n > 0: at most n runes; leading and trailing white space are removed
n == 0: no text
n < 0: all text

func (*Template) MustRun added in v0.31.0

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

MustRun is like Run but panics if the execution fails.

func (*Template) Run added in v0.31.0

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

Run runs the template and write the rendered code to out. vars contains the values of the global variables.

func (*Template) UsedVars added in v0.32.0

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

UsedVars returns the names of the global variables used in the template.

Jump to

Keyboard shortcuts

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