Overview
DocuGen AI uses Jinja2 as its template engine to generate README files. The template system is flexible, allowing you to customize the output format and structure of generated documentation.Template Engine Architecture
TheTemplateEngine class (docugen/templates/engine.py:11) manages template rendering:
Configuration Options
| Option | Value | Purpose |
|---|---|---|
autoescape | False | Allows raw Markdown output without HTML escaping |
trim_blocks | True | Removes newlines after template blocks |
lstrip_blocks | True | Strips whitespace before blocks for cleaner output |
Default Template Structure
The default template (default_readme.md.j2) receives three context variables:Template Context Variables
The name of the project being documented
AI-generated technical documentation from Gemini API
Project analysis metrics including:
file_count: Number of Python files scannedclass_count: Total classes foundfunction_count: Top-level functionsmethod_count: Methods within classeserror_count: Parsing errors encountered
Creating Custom Templates
Step 1: Create Template File
Create a new.j2 file in your templates directory:
custom_readme.md.j2
Step 2: Use Custom Template
Advanced Customization
Adding Custom Filters
Extend the Jinja2 environment with custom filters:Conditional Sections
Add logic-based content rendering:Loops and Iteration
If you extend the context with additional data:Template Best Practices
Keep Templates Simple
Templates should focus on structure and formatting. Leave complex logic in Python code.
Use Whitespace Control
Leverage
trim_blocks and lstrip_blocks to keep generated Markdown clean.Provide Fallbacks
Always use conditional checks (
{% if summary %}) for optional data.Test Thoroughly
Test templates with various data scenarios including missing fields and edge cases.
Rendering API
Therender() method (docugen/templates/engine.py:21) handles template execution:
The method automatically strips whitespace and ensures the output ends with a single newline.
Common Use Cases
Multi-Format Output
Create templates for different documentation formats:readme.md.j2- Standard READMEapi_docs.md.j2- API reference formatchangelog.md.j2- Change log formatwiki.md.j2- Wiki-style documentation
Internationalization
Create language-specific templates:readme_es.md.j2
