gotpl
A Rust library for using Go's powerful text/template and html/template engines via FFI. Get the safety of Rust with the mature templating power of Go.
✨ Features
- Full Go Template Support: Complete support for Go's
text/templateandhtml/templatesyntax. - HTML Safety: Automatic HTML escaping for XSS prevention via
html/template. - Flexible Data: Render templates with any
serde::Serialize-able Rust data. - Idiomatic Errors: Go template errors are converted into Rust
Resulttypes. - Memory Safe: Memory allocated by Go is safely managed and freed from Rust to prevent leaks.
- Lightweight: Depends only on
serdefor data serialization.
🚀 Quick Start
Installation
Add to your Cargo.toml:
[]
= "0.2.3" # Use the latest version
= { = "1.0", = ["derive"] }
= "1.0"
Example
use TemplateRenderer;
use json;
🌐 Go Template Syntax
This library supports the complete Go template syntax. For details, see the official docs:
🛠️ Build Process
gotpl uses a build.rs script to compile the Go source into a static library and generate Rust FFI bindings with bindgen.
Requirements:
- Go Compiler (version 1.18+)
- Rust Toolchain (Cargo)
The build is fully automated when you run cargo build.
⚠️ Caveats
- Performance: FFI calls have overhead. Profile accordingly for high-performance use cases.
- Binary Size: Includes the Go runtime, which will increase your final binary size.
- Memory: All memory is managed safely across the FFI boundary.
🤝 Contributing
Contributions are welcome via Pull Requests or Issues.
📜 License
Licensed under the MIT License. See LICENSE for details.