renderer package provides HTML template rendering with thread-safe asset management, template caching, and optional minification.
Overview
Key features:- Template Caching: Global template cache with modification time tracking
- Asset Management: Thread-safe asset map for hashed CSS/JS filenames
- Compression: Optional HTML/CSS/JS minification
- Custom Functions: Template helpers for common operations
- File Tracking: Tracks all rendered files for cleanup operations
Creating a Renderer
compress- Enable HTML minificationdestFs- Destination filesystem (for testing with in-memory FS)templateDir- Path to template directory (e.g.,themes/docs/templates)logger- Structured logger
Renderer Type
Core Methods
RenderPage
Asset Management
SetAssets
GetAssets
File Tracking
RegisterFile
RenderPage).
GetRenderedFiles
ClearRenderedFiles
Template Functions
The renderer provides custom template functions:Template Caching
Templates are cached globally with modification time tracking:Template Structure
Required Templates
layout.html - Base layout with content block:Optional Templates
index.html - Homepage template:Compression
WhenCompress is enabled, HTML is minified using:
- Remove comments and whitespace
- Collapse inline CSS/JS
- Optimize attributes
- ~20-30% size reduction
Error Handling
The renderer logs errors with structured logging:Performance Optimization
Buffered Writing
Template Precompilation
Templates are compiled once and cached:Thread Safety
All public methods are thread-safe:Assetsmap protected byAssetsMu(RWMutex)RenderedSetprotected byRenderedMu(RWMutex)- Template cache uses global
sync.RWMutex