Documentation ¶
Overview ¶
Package montemplates parses HTML templates from a file tree and optionally monitors for changes.
During construction, it parses all the templates under a root directory, traversing subdirectories for files with the given patterns.
At execution time, if `dynamic` is set to true, at every request (`Get()` method) it checks whether files have changed, and re-parses them accordingly. This is very useful during development, but you want to turn it off during production because of the cost of checking whether the files changed in the filesystem.
If `dynamic==true` it does proper serialization (`sync.Mutex`) to prevent concurrency conflicts. If `dynamic==false` it is read-only and there is no contention.
## Example
import ( montemplates "github.com/janpfeifer/monitored-templates" ) flagDynamicTemplates = flag.Bool("dynamic_templates", false, "If set, template files are checked at every access to checks for changes. "+ "Slow, leave this disabled for production.") func main() { ... templateSet, err := montemplates.New( rootTemplatesPath, // Path searched for template files. []string{"*.html", "*.js", "*.css"}, // File patterns to be read into templates. *flagDynamicTemplates) // If true, files are monitored for changes and re-parsed accordingly. ... h := func (w http.ResponseWriter, req *http.Request) { t, err := templateSet.Get("nav/login.html") // Will re-read the file if changed err = t.Execute(w, ...) } ... }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Collection ¶
type Collection struct {
// contains filtered or unexported fields
}
Collection manages all templates under a certain directory.
func New ¶
func New(root string, patterns []string, dynamic bool) (collection *Collection, err error)
New creates a Collection with parsed templates (files) from a directory.
If you need more control, use instead the Build API to create a new Collection.
The `root` directory is recursively traversed and every file with the given `patterns`.
Each pattern is checked against the file name (without the path) of each file under `root`, with the same semantics as `filepath.Match`. Notice this doesn't allow directory patterns to be matched (a limitation with filepath.Match).
If `dynamic` is used at every call to `Get()` for a template, it will check whether files are changed, and update accordingly. This also has the side effect of running much slower, so likely something only used for development.
func (*Collection) Get ¶
func (c *Collection) Get(name string) (*template.Template, error)
Get returns the named template.
If `dynamic` was set during build time, and any file changed, it will re-parse all templates.
Notice that since montemplates has no access to the template dependency graph, all template files need checking for updates.
func (*Collection) Template ¶
func (c *Collection) Template() *template.Template
Template returns one of the underlying templates. This can be useful to enumerate all the templates with `c.Template().Templates()`.
type CollectionConfig ¶ added in v0.1.0
type CollectionConfig struct {
// contains filtered or unexported fields
}
CollectionConfig is the configuration created by Build and used to create a new Collection.
func Build ¶ added in v0.1.0
func Build(root string, patterns []string) (config *CollectionConfig)
Build starts a configuration to build a collection. Call Done when everything is configured.
The `root` directory is recursively traversed and every file with the given `patterns`.
Each pattern is checked against the file name (without the path) of each file under `root`, with the same semantics as `filepath.Match`. Notice this doesn't allow directory patterns to be matched (a limitation with filepath.Match).
func (*CollectionConfig) Done ¶ added in v0.1.0
func (config *CollectionConfig) Done() (collection *Collection, err error)
Done finishes configuration of a Collection and builds it, or an error if something went wrong.
func (*CollectionConfig) Dynamic ¶ added in v0.1.0
func (config *CollectionConfig) Dynamic(enabled bool) *CollectionConfig
Dynamic configures whether at every call to `Get()` for a template, it checks whether files are changed, and update accordingly. This also has the side effect of the template collection running much slower, so likely something only used for development.
func (*CollectionConfig) WithFuncs ¶ added in v0.1.0
func (config *CollectionConfig) WithFuncs(funcMap template.FuncMap) *CollectionConfig
WithFuncs defines the given functions in the templates, before parsing them. It can be called multiple times with different sets of functions: if the same name is used it will override any previous definition.