Version: v0.12.5 Latest Latest

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

Go to latest
Published: Mar 4, 2020 License: MIT Imports: 13 Imported by: 0



Package view is implementation of aah framework view engine using Go Template engine. It supports multi-layouts, no-layout, partial inheritance and error pages.



This section is empty.


View Source
var (
	// TemplateFuncMap aah framework Go template function map.
	TemplateFuncMap = make(template.FuncMap)

	// DefaultDelimiter template default delimiter
	DefaultDelimiter = "{{.}}"
View Source
var (
	ErrTemplateEngineIsNil = errors.New("view: engine value is nil")
	ErrTemplateNotFound    = errors.New("view: template not found")
	ErrTemplateKeyExists   = errors.New("view: template key exists")

view error messages


func AddEngine

func AddEngine(name string, engine Enginer) error

AddEngine method adds the given name and engine to view store.

func AddTemplateFunc

func AddTemplateFunc(funcMap template.FuncMap)

AddTemplateFunc method adds given Go template funcs into function map.

func StripPathPrefixAt

func StripPathPrefixAt(str, pathCut string) string

StripPathPrefixAt method strips the given path to path cut position.

For Example:

path := "/Users/jeeva/go/src/"
result := StripPrefixAt(path, "views/")

func TrimPathPrefix

func TrimPathPrefix(prefix string, fpaths ...string) string

TrimPathPrefix method trims given file paths by prefix and returns comma separated string.


type EngineBase

type EngineBase struct {
	CaseSensitive   bool
	IsLayoutEnabled bool

	Name       string
	BaseDir    string
	FileExt    string
	LeftDelim  string
	RightDelim string
	AppConfig  *config.Config
	Templates  map[string]*Templates
	VFS        *vfs.VFS
	// contains filtered or unexported fields

EngineBase struct is to create common and repurpose the implementation. Could be used for custom view engine implementation.

func (*EngineBase) AddTemplate

func (eb *EngineBase) AddTemplate(layout, key string, tmpl *template.Template) error

AddTemplate method adds the given template for layout and key.

func (*EngineBase) AutoFieldInsertion

func (eb *EngineBase) AutoFieldInsertion(name, v string) string

AutoFieldInsertion method processes the aah view's to auto insert the field.

func (*EngineBase) DirsPath

func (eb *EngineBase) DirsPath(subDir string) ([]string, error)

DirsPath method returns all sub directories from `<view-base-dir>/<sub-dir-name>`. if it not exists returns error.

func (*EngineBase) FilesPath

func (eb *EngineBase) FilesPath(subDir string) ([]string, error)

FilesPath method returns all file path from `<view-base-dir>/<sub-dir-name>`. if it not exists returns error.

func (*EngineBase) Get

func (eb *EngineBase) Get(layout, tpath, tmplName string) (*template.Template, error)

Get method returns the template based given name if found, otherwise nil.

func (*EngineBase) Init

func (eb *EngineBase) Init(fs *vfs.VFS, appCfg *config.Config, baseDir, defaultEngineName, defaultFileExt string) error

Init method is to initialize the base fields values.

func (*EngineBase) LayoutFiles

func (eb *EngineBase) LayoutFiles() ([]string, error)

LayoutFiles method returns the all layout files from `<view-base-dir>/layouts`. If layout directory doesn't exists it returns error.

func (*EngineBase) NewTemplate

func (eb *EngineBase) NewTemplate(key string) *template.Template

NewTemplate method return new instance on `template.Template` initialized with key, template funcs and delimiters.

func (*EngineBase) Open

func (eb *EngineBase) Open(filename string) (string, error)

Open method reads template from VFS if not found resolve from physical file system. Also does auto field insertion such as Anti-CSRF(anti_csrf_token) and requested page URL (_rt).

func (*EngineBase) ParseErrors

func (eb *EngineBase) ParseErrors(errs []error) error

ParseErrors method to parse and log the template error messages.

func (*EngineBase) ParseFile

func (eb *EngineBase) ParseFile(filename string) (*template.Template, error)

ParseFile method parses given single file.

func (*EngineBase) ParseFiles

func (eb *EngineBase) ParseFiles(t *template.Template, filenames ...string) (*template.Template, error)

ParseFiles method parses given files with given template instance.

func (*EngineBase) SetHotReload

func (eb *EngineBase) SetHotReload(r bool)

SetHotReload method set the view engine mode into hot reload without watcher.

type Enginer

type Enginer interface {
	Init(fs *vfs.VFS, appCfg *config.Config, baseDir string) error
	Get(layout, path, tmplName string) (*template.Template, error)

Enginer interface defines a methods for pluggable view engine.

func GetEngine

func GetEngine(name string) (Enginer, bool)

GetEngine method returns the view engine from store by name otherwise nil.

type GoViewEngine

type GoViewEngine struct {

GoViewEngine implements the partial inheritance support with Go templates.

func (*GoViewEngine) Init

func (e *GoViewEngine) Init(fs *vfs.VFS, appCfg *config.Config, baseDir string) error

Init method initialize a template engine with given aah application config and application views base path.

type Templates

type Templates struct {
	// contains filtered or unexported fields

Templates hold template reference of lowercase key and case sensitive key with reference to compliled template.

func (*Templates) Add

func (t *Templates) Add(key string, tmpl *template.Template) error

Add method adds the given template for the key.

func (*Templates) IsExists

func (t *Templates) IsExists(key string) bool

IsExists method returns true if template key exists otherwise false.

func (*Templates) Keys

func (t *Templates) Keys() []string

Keys method returns all the template keys.

func (*Templates) Lookup

func (t *Templates) Lookup(key string) *template.Template

Lookup method return the template for given key.

Jump to

Keyboard shortcuts

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