Overview
XTemplatextemplate is a hypertext preprocessor that extends Go’s html/template library to be capable enough to host an entire server-side web application using just template definitions. It is designed with the htmx.org JS library in mind, which makes server-side rendered sites feel as interactive as Single Page Apps. However, it is important to note that this project is still in development.
Features
- Query the database directly within template definitions, with automatic input sanitization to prevent basic XSS attacks.
- Define templates and import content from other files.
- File-based routing, where GET requests for a path with a matching template file will invoke the template file at that path, excluding files starting with “_”.
- Create custom routes with parametrized paths using the <method> <path> pattern, with path parameters and wildcards available as values in the template.
- Automatic reload of templates, with validation, as soon as they are modified, eliminating the need to restart the server.
Installation
To use XTemplatextemplate, you will need to include the following packages in your Go application:
github.com/infogulch/xtemplate: a library that loads template files and implementshttp.Handler. Use it by depending on it as a library and utilizing theXTemplatestruct.github.com/infogulch/xtemplate/bin: a simple binary that configures XTemplate with CLI args and serves HTTP requests. Build it yourself or download the binary from GitHub releases.github.com/infogulch/xtemplate/caddy: a Caddy module that integrates XTemplate into the Caddy web server.
Summary
XTemplatextemplate is a tool that extends Go’s html/template library to enable the creation of server-side web applications using only template definitions. It offers features such as querying the database within templates, importing content from other files, file-based routing, custom routes, and automatic template reloading. Although this project is still in development, it shows promise in simplifying web application development and making server-side rendered sites more interactive.