Templates are the implementation for your generator. They are parameterized files which are filled in with data from the schema and user input.
Generators have several kinds of files that end up in the output
- Once Templates - used to generate a single file, like
- Repeated Templates - generate a file for each element, like routes in this example
- Partial Templates - reusable template snippets which are available in all full templates
- Static Files - copied directly into the output, bypassing the template engine
- Config Files - generate yaml or json into the output, bypassing the template engine
Templates are based on Go
text/template with extra helpers and conventions.
We will cover the basics in the first-example and they should be familiar to other text templating systems.
Read [template writing(/code-generation/template-writing/) to learn more about the details.
These files are needed once for every server we generate.
Some have minimal templating and others loop over values, like
Repeated and Partial Templates
We separate the handler into a template which uses the partial. This is for demonstration purpose here and will be more useful in the “full-example” section where the implementation is more complete.
By default, anything in the
static/ dir will be copied into the output dir.
You can configure one or more manually via the generator by setting the
Statics field on your generator.
Rendered Output Files
Here we can see the result of code generation for a sample of the files. We will actually generate these in the next section. They are provided here so you can see the input / output pairs on a single page.