template

package
v0.0.0-...-712f093 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2015 License: GPL-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ERROR_CLASS = "hasError"
View Source
var (
	// The functions available for use in the templates.
	TemplateFuncs = map[string]interface{}{
		"set": func(renderArgs map[string]interface{}, key string, value interface{}) template.JS {
			renderArgs[key] = value
			return template.JS("")
		},
		"append": func(renderArgs map[string]interface{}, key string, value interface{}) template.JS {
			if renderArgs[key] == nil {
				renderArgs[key] = []interface{}{value}
			} else {
				renderArgs[key] = append(renderArgs[key].([]interface{}), value)
			}
			return template.JS("")
		},
		"firstof": func(args ...interface{}) interface{} {
			for _, val := range args {
				switch val.(type) {
				case nil:
					continue
				case string:
					if val == "" {
						continue
					}
					return val
				default:
					return val
				}
			}
			return nil
		},

		"pad": func(str string, width int) template.HTML {
			if len(str) >= width {
				return template.HTML(html.EscapeString(str))
			}
			return template.HTML(html.EscapeString(str) + strings.Repeat(" ", width-len(str)))
		},

		"dump": func(v interface{}) template.HTML {
			return template.HTML(fmt.Sprintf("%v", v))
		},

		"goto": func(uri string) template.JS {
			script := `<script language="javascript">window.location.href="%s";</script>`
			return template.JS(fmt.Sprintf(script, uri))
		},

		"nl2br": func(text string) template.HTML {
			return template.HTML(strings.Replace(template.HTMLEscapeString(text), "\n", "<br>", -1))
		},

		"raw": func(text string) template.HTML {
			return template.HTML(text)
		},

		"date": func(date time.Time) string {
			return fmt.Sprintf("%4d-%02d-%02d", date.Year(), date.Month(), date.Day())
		},
		"datetime": func(date time.Time) string {
			return fmt.Sprintf("%4d-%02d-%02d %02d:%02d:%02d", date.Year(), date.Month(), date.Day(),
				date.Hour(), date.Minute(), date.Second())
		},
		"even": func(a int) bool { return (a % 2) == 0 },
	}
)

Functions

func Function

func Function(name string, fn interface{})

func ParseTemplateError

func ParseTemplateError(err error) (templateName string, line int, description string)

Parse the line, and description from an error message like: html/template:Application/Register.html:36: no such template "footer.html"

func ReadLines

func ReadLines(filename string) ([]string, error)

Reads the lines of the given file. Panics in the case of error.

Types

type Error

type Error struct {
	SourceType               string   // The type of source that failed to build.
	Title, Path, Description string   // Description of the error, as presented to the user.
	Line, Column             int      // Where the error was encountered.
	SourceLines              []string // The entire source file, split into lines.
	Stack                    string   // The raw stack trace string from debug.Stack().
	MetaError                string   // Error that occurred producing the error page.
	Link                     string   // A configurable link to wrap the error source in
}

An error description, used as an argument to the error template.

func (*Error) ContextSource

func (e *Error) ContextSource() []sourceLine

Returns a snippet of the source around where the error occurred.

func (*Error) Error

func (e *Error) Error() string

Construct a plaintext version of the error, taking account that fields are optionally set. Returns e.g. Compilation Error (in views/header.html:51): expected right delim in end; got "}"

type GoTemplate

type GoTemplate struct {
	*template.Template
	// contains filtered or unexported fields
}

Adapter for Go Templates.

func (GoTemplate) Content

func (gotmpl GoTemplate) Content() []string

func (GoTemplate) Render

func (gotmpl GoTemplate) Render(wr io.Writer, arg interface{}) error

return a 'revel.Template' from Go's template.

type Template

type Template interface {
	Name() string
	Content() []string
	Render(wr io.Writer, arg interface{}) error
}

type TemplateLoader

type TemplateLoader struct {
	log.Logger
	// contains filtered or unexported fields
}

This object handles loading and parsing of templates. Everything below the application's views directory is treated as a template.

func NewTemplateLoader

func NewTemplateLoader(delimiters string, paths []string, logger log.Logger) *TemplateLoader

func (*TemplateLoader) Load

func (loader *TemplateLoader) Load() error

func (*TemplateLoader) Refresh

func (loader *TemplateLoader) Refresh() *Error

This scans the views directory and parses all templates as Go Templates. If a template fails to parse, the error is set on the loader. (It's awkward to refresh a single Go Template)

func (*TemplateLoader) Template

func (loader *TemplateLoader) Template(name string) (Template, error)

Return the Template with the given name. The name is the template's path relative to a template loader root.

An Error is returned if there was any problem with any of the templates. (In this case, if a template is returned, it may still be usable.)

func (*TemplateLoader) WatchDir

func (loader *TemplateLoader) WatchDir(info os.FileInfo) bool

func (*TemplateLoader) WatchFile

func (loader *TemplateLoader) WatchFile(basename string) bool

Jump to

Keyboard shortcuts

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