Explore Templates
Now let's examine the templates that will generate our database documentation. Understanding how templates work is key to creating your own custom code generation.
📁 Template Structure¶
The tutorial uses two main files in the tutorial-database-documenter
folder:
table.tmpl
- The Go template that generates markdown documentationtemplates.json
- Configuration file that tells Monstarillo what to do
🔧 Configuration Breakdown¶
Let's examine the templates.json
configuration:
{
"templates": [
{
"templateFile": "{{getTag .Tags \"TemplateRoot\"}}/tutorial-database-documenter/table.tmpl",
"generatedFileName": "{{ .CurrentTable.GetCamelCaseTableName}}.md",
"generatedFolderName": "tables",
"minimumGeneratedFileLength": 0,
"outputPath": "{{getTag .Tags \"OutputPath\"}}/",
"overwriteFile": true
}
],
"tags": [
{
"tagName": "TemplateRoot",
"value": "/home/patrick/code/templates"
},
{
"tagName": "OutputPath",
"value": "/home/patrick/code-gen-output/postgres-chinhook-db-md"
}
]
}
Template Configuration Properties¶
Property | Value | Purpose |
---|---|---|
templateFile |
{{getTag .Tags "TemplateRoot"}}/tutorial-database-documenter/table.tmpl |
Path to the template file using the TemplateRoot tag |
generatedFileName |
{{ .CurrentTable.GetCamelCaseTableName}}.md |
Dynamic filename based on table name (e.g., Album.md ) |
generatedFolderName |
tables |
Subdirectory to organize generated files |
outputPath |
{{getTag .Tags "OutputPath"}}/ |
Root output directory using OutputPath tag |
overwriteFile |
true |
Replace existing files if they exist |
minimumGeneratedFileLength |
0 |
Create files regardless of size (0 = no minimum) |
Configuration Tags¶
Tags are reusable variables that keep your configuration flexible:
Tag | Purpose | Example Value |
---|---|---|
TemplateRoot |
Location of template files | /Users/you/monstarillo-templates |
OutputPath |
Where to save generated files | /Users/you/output |
Dynamic Values
Notice how the configuration uses template variables:
- {{getTag .Tags "TemplateRoot"}}
- References the TemplateRoot tag
- {{ .CurrentTable.GetCamelCaseTableName}}
- Uses table metadata for filenames
📄 The Template File¶
The table.tmpl
file contains Go template syntax that generates markdown documentation. Here's what it does:
Template Purpose¶
- Input: Database table metadata (columns, types, relationships)
- Output: Formatted markdown documentation for each table
- Result: Human-readable database documentation
Template Variables Available¶
The template has access to rich database metadata through the Monstarillo context:
🎯 Generation Process¶
When Monstarillo runs, here's what happens:
- Connect to your database
- Extract metadata for all tables
- Loop through each table:
- Set
CurrentTable
to the current table being processed - Execute
table.tmpl
with table data - Generate
TableName.md
file inoutputPath/tables/
- Repeat for every table in the database
Example Output¶
For a table named album
, Monstarillo will:
- Process the album
table metadata
- Generate a file named Album.md
- Save it to [OutputPath]/tables/Album.md
- Include column information, data types, and relationships
🚀 Next Steps¶
Ready to see this in action? Let's run Monstarillo and generate our database documentation:
💡 Template Customization Ideas¶
Once you understand the basics, you can create templates for:
- REST API endpoints (Go, Java, Node.js)
- Database models (Sequelize, GORM, Hibernate)
- GraphQL schemas
- API documentation (OpenAPI/Swagger)
- Test files with realistic data
The API Reference documents all available template variables and methods.