Jinja Template Renderer
How AST-Powered Mock Generation Eliminated Manual Test Data for Jinja Templates
En bref
Défi
Previewing Jinja templates required wiring up real backends or writing manual test data
Résultat
Instant live-reload previews with auto-generated mock data from AST parsing
Technologies
Statut
En cours
Situation
Frappe developers building custom print formats, email templates, and report layouts in Jinja2 had no way to preview their work without connecting to a real Frappe instance. Testing a template change meant creating real documents in the system, which was slow and cumbersome. Alternatively, developers would write manual JSON mock data files, but keeping them in sync with evolving templates was error-prone and tedious.
Le défi
Build a standalone renderer that automatically understands what data a Jinja template expects — and generates matching mock data on the fly — so developers can preview and iterate on templates instantly without any backend.
Ce qui a été construit
Built an AST parser that walks the Jinja2 template tree to extract all variable paths, iterable references, and loop variable mappings — using the actual parser, not fragile regex patterns.
Implemented automatic mock data generation: the parser infers variable types from usage context and generates plausible values (strings, numbers, dates, lists) without manual configuration.
Created DocMock, a Frappe-aware mock object that simulates Frappe document fields and methods, enabling realistic previews of print formats and report templates.
Added a live-reload development server with file watching — changes to templates or data files trigger instant browser refresh via injected polling.
Included French-locale number formatting support and JSON context export for debugging and sharing template data.
Résultats
Template preview setup
Real Frappe instance required
Standalone, zero dependencies
Mock data creation
Manual JSON files
Auto-generated from AST
Feedback loop
Live-reload on file save
Template features
Includes, extends, macros, loops
Frappe template developers now iterate on print formats and reports with instant visual feedback and zero backend dependencies — the tool understands what data each template needs and generates it automatically.
Réalisation clé
Live-reload template previewing with auto-generated contextual mock data including Frappe-specific DocMock objects, eliminating all manual test data setup.