Documentation ¶
Overview ¶
Package templates compiles all templates in a directory tree into a Service that provides a Render method to execute other templates inside its context.
Example ¶
The following example program will load a template directory tree defined as a constant and then render a template file from another directory using the loaded Service into the standard output.
package main import ( "os" "path" "github.com/leonelquinteros/thtml/templates" ) const ( _templates = "/path/to/templates" _public = "/path/to/web/root" ) func main() { tplService, err := templates.Load(_templates) if err != nil { panic(err.Error()) } tplService.Render(os.Stdout, path.Join(_public, "index.html"), nil) }
Index ¶
- Variables
- func BuildID() string
- func ID() string
- type EmptyTemplateError
- type IError
- type Service
- func (s *Service) AddExtension(ext string)
- func (s *Service) Build(in, out string) (err error)
- func (s *Service) Load(dir string) error
- func (s *Service) Minify(m bool)
- func (s *Service) RemoveExtension(ext string)
- func (s *Service) Render(w io.Writer, filename string, data interface{}) error
- func (s *Service) ValidExtension(ext string) bool
- type TError
Constants ¶
This section is empty.
Variables ¶
var FuncMap = template.FuncMap{ "ID": ID, "BuildID": BuildID, }
FuncMap is passed to the template object that renders every view
Functions ¶
Types ¶
type EmptyTemplateError ¶
type EmptyTemplateError struct { // Error composition TError }
EmptyTemplateError is returned when templates.Service hasn't been loaded.
func NewEmptyTemplateError ¶
func NewEmptyTemplateError() EmptyTemplateError
NewEmptyTemplateError returns a new EmptyTemplateError object
type IError ¶
type IError interface { // errors.Error implementation Error() string // Stringer implementation String() string // Dump stack trace Stack() string // Creation time Created() time.Time }
IError is the common error type interface.
type Service ¶
Service is the template handler. After Load()'ing a directory tree, will be ready to render any template into its context.
func Load ¶
Load creates a new *templates.Service object and loads the templates in the provided directory. Custom set of filename extensions can be supplied
func (*Service) AddExtension ¶
AddExtension adds a new filename extension (i.e. ".txt") to the list of extensions to support. Extensions not supported will be rendered and/or compiled as they are without template parsing. This method is safe to use from multiple/concurrent goroutines.
func (*Service) Build ¶
Build compiles all files in the provided directory and outputs the results to the build dir. This method is NOT safe to use from multiple/concurrent goroutines
func (*Service) Load ¶
Load takes a directory path and loads all templates on it. This method is NOT safe to use from multiple/concurrent goroutines
func (*Service) RemoveExtension ¶
RemoveExtension deletes a supported filename extension from the list. This method is safe to use from multiple/concurrent goroutines.
func (*Service) Render ¶
Render compiles the provided template filename in the loaded templates and writes the output to the provided io.Writer. This method is safe to use from multiple/concurrent goroutines
func (*Service) ValidExtension ¶
ValidExtension returns true if the filename extension provided is supported. This method is safe to use from multiple/concurrent goroutines.