Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/janpfeifer/monitored-templates
Go library that parses HTML templates from file tree, and optionally dynamically monitors for changes, updating accordingly
https://github.com/janpfeifer/monitored-templates
Last synced: about 2 months ago
JSON representation
Go library that parses HTML templates from file tree, and optionally dynamically monitors for changes, updating accordingly
- Host: GitHub
- URL: https://github.com/janpfeifer/monitored-templates
- Owner: janpfeifer
- License: apache-2.0
- Created: 2024-01-07T11:43:31.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-02-17T06:26:00.000Z (10 months ago)
- Last Synced: 2024-10-14T21:58:04.616Z (2 months ago)
- Language: Go
- Size: 21.5 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Monitored Templates
Minimalistic Go library that parses HTML templates from a file tree, and 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 or for simple/cheap "HMR" (Hot Module Reload).Probably you want to turn it off during production because
of the cost of checking whether the files changed in the filesystem.
But if you want other alternatives for "HMR" -- e.g.: asynchronous polling for changes every so often --
please create a feature request, it would be easy to implement.If `dynamic==true` it does proper serialization (`sync.Mutex`) to prevent concurrency conflicts.
If `dyncamic==false` it is read-only and there is no contention.## Example
```go
package mainimport (
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 as templates.
*flagDynamicTemplates) // If true, files are monitored for changes and re-parsed accordingly.
...
loginHandler := func (w http.ResponseWriter, req *http.Request) {
...
t, err := templateSet.Get("nav/login.html") // Re-parses the file if changed
err = t.Execute(w, ...)
if err != nil { ... }
}
...
http.Handle("/login", loginHandler)
}
```