Documentation
¶
Overview ¶
Package template provides shared rendering primitives for Fiber template engines.
Index ¶
- func AcquireViewContext(binding interface{}) map[string]interface{}
- func HasExtension(path, extension string) bool
- func NewViewContext(binding interface{}, extra int) map[string]interface{}
- func RangeViewContext(binding interface{}, fn func(key string, value interface{}))
- func ReadFile(path string, fs http.FileSystem) ([]byte, error)
- func TemplateName(directory, path, extension string) (string, error)
- func ViewContextLen(binding interface{}) int
- func Walk(fs http.FileSystem, directory string, ...) error
- type Engine
- func (e *Engine) AddFunc(name string, fn interface{}) IEngineCore
- func (e *Engine) AddFuncMap(m map[string]interface{}) IEngineCore
- func (e *Engine) Debug(enabled bool) IEngineCore
- func (e *Engine) Delims(left, right string) IEngineCore
- func (e *Engine) FuncMap() map[string]interface{}
- func (e *Engine) Layout(key string) IEngineCore
- func (e *Engine) PreRenderCheck() bool
- func (e *Engine) Reload(enabled bool) IEngineCore
- type IEngine
- type IEngineCore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AcquireViewContext ¶
func AcquireViewContext(binding interface{}) map[string]interface{}
AcquireViewContext ensures the binding value is represented as a map[string]interface{} so template engines can safely inject layout specific data while preserving the original values. It supports native map[string]interface{} values as well as user-defined map types (for example fiber.Map) with string keys.
func HasExtension ¶ added in v2.1.1
HasExtension reports whether path is a template file for extension: it has to end in extension and carry a name in front of it.
func NewViewContext ¶ added in v2.1.1
NewViewContext resolves binding like AcquireViewContext, but always returns a map the caller owns, sized for extra entries beyond the binding's own.
func RangeViewContext ¶ added in v2.1.1
func RangeViewContext(binding interface{}, fn func(key string, value interface{}))
RangeViewContext resolves binding like AcquireViewContext and hands every key/value pair to fn.
func ReadFile ¶ added in v2.1.0
func ReadFile(path string, fs http.FileSystem) ([]byte, error)
ReadFile reads a file from the file system or http.FileSystem. This wrapper provides a centralized abstraction point for file operations, allowing template engines to depend only on the core package while the core manages the underlying utils dependency.
func TemplateName ¶ added in v2.1.1
TemplateName derives the name a template file is registered under: ./views/partials/footer.html relative to ./views becomes partials/footer.
func ViewContextLen ¶ added in v2.1.1
func ViewContextLen(binding interface{}) int
ViewContextLen reports how many key/value pairs RangeViewContext yields for binding, so engines can size their own context type before filling it.
func Walk ¶ added in v2.1.0
func Walk(fs http.FileSystem, directory string, walkFn func(path string, info os.FileInfo, err error) error) error
Walk walks the file tree rooted at directory, calling walkFn for each file or directory in the tree, including directory. This wrapper provides a centralized abstraction point for filesystem traversal, allowing template engines to depend only on the core package while the core manages the underlying utils dependency.
Types ¶
type Engine ¶
type Engine struct {
IEngineCore
// delimiters
Left string
Right string
// views folder
Directory string
// http.FileSystem supports embedded files
FileSystem http.FileSystem
// views extension
Extension string
// layout variable name that incapsulates the template
LayoutName string
// determines if the engine parsed all templates
Loaded bool
// reload on each render
ShouldReload bool
// debug prints the parsed templates
Verbose bool
// lock for funcmap and templates
Mutex sync.RWMutex
// template funcmap
Funcmap map[string]interface{}
}
Engine engine struct
func (*Engine) AddFunc ¶
func (e *Engine) AddFunc(name string, fn interface{}) IEngineCore
AddFunc adds the function to the template's function map. It is legal to overwrite elements of the default actions
func (*Engine) AddFuncMap ¶
func (e *Engine) AddFuncMap(m map[string]interface{}) IEngineCore
AddFuncMap adds the functions from a map to the template's function map. It is legal to overwrite elements of the default actions
func (*Engine) Debug ¶
func (e *Engine) Debug(enabled bool) IEngineCore
Debug will print the parsed templates when Load is triggered.
func (*Engine) Delims ¶
func (e *Engine) Delims(left, right string) IEngineCore
Delims sets the action delimiters to the specified strings, to be used in templates. An empty delimiter stands for the corresponding default: "{{" and "}}".
func (*Engine) Layout ¶
func (e *Engine) Layout(key string) IEngineCore
Layout defines the variable name that will incapsulate the template
func (*Engine) PreRenderCheck ¶
PreRenderCheck determines if the engine should reload the templates before rendering. Explicit mutex unlock vs defer offers better performance.
The steady state - loaded, reloading off - is answered under the shared lock.
func (*Engine) Reload ¶
func (e *Engine) Reload(enabled bool) IEngineCore
Reload if set to true the templates are reloading on each render, use it when you're in development and you don't want to restart the application when you edit a template file.
type IEngine ¶
type IEngine interface {
IEngineCore
Load() error
Render(out io.Writer, template string, binding interface{}, layout ...string) error
}
IEngine interface, to be implemented for any templating engine added to the repository
type IEngineCore ¶
type IEngineCore interface {
AddFunc(name string, fn interface{}) IEngineCore
AddFuncMap(m map[string]interface{}) IEngineCore
Debug(enabled bool) IEngineCore
Delims(left, right string) IEngineCore
FuncMap() map[string]interface{}
Layout(key string) IEngineCore
Reload(enabled bool) IEngineCore
PreRenderCheck() bool
}
IEngineCore interface


