Extend Template
Gleece is designed with customization at its core. The most maintainable way to customize templates is through template extension.
Gleece provides a comprehensive set of core templates for each engine to generate code. These templates serve as a foundation that you can extend to match your specific requirements.
Template extension is particularly valuable when you need to incorporate custom functionality such as:
- Adding telemetry
- Implementing logging
- Handling service-specific common logic
- Go wild...
One of the key benefits of using Gleece template extensions is that they remain compatible with core template updates. When the core templates are updated, your extensions will continue to work without requiring manual merges.
Create a new template
To create an extension, start by creating a new hbs
file that will contain your custom template logic.
Here's a practical example that demonstrates how to add console logging for successful operation results:
Gleece uses Handlebars as the templating format through the raymond engine.
For more information about Handlebars syntax and features, visit handlebarsjs.com.
Set the extension
Configure your extension in the gleece.config.json
file by specifying the AfterOperationRoutesExtension
and its corresponding file path:
{
"routesConfig": {
"templateExtensions": {
"AfterOperationRoutesExtension": "./after.operation.extension.hbs"
}
}
}
You can find all available template extensions in the engine-specific embeds.go
file under TemplateExtensions
map. For example, check the gin engine's embeds.go.
If you specify an invalid extension name, during the build Gleece will respond with an error message listing all available extension names!
Generate with the extension
Run the Gleece
build to regenerate routes.
During the build process, your extended template becomes fully integrated with the core template, giving you access to the complete operation context and all available template variables.
Locating Extension In Core Templates:
- Look for the extension name in the core templates
- See the exact insertion point and available context variables (like
OperationId
)
Locating Extension In Generated Code:
- Run the Gleece CLI
- Search for placeholder comments in the generated code (e.g.,
// after operation routes extension placeholder
). These comments come from the embeds.go file and mark where your extension code will be inserted
Want to go deeper into template context? Check out context.go to discover all available properties.