Closed jace closed 7 months ago
Sample output from flask shell, showing the template files and line numbers where variables are referenced without being supplied in the context:
>>> from pprint import pprint
>>> from funnel.views.index import IndexTemplate
>>> pprint(IndexTemplate.jinja_validate_vars())
{'index.html.jinja2': set(),
'js/schedule.js.jinja2': set(),
'layout.html.jinja2': {(15, 'title'),
(18, 'title_suffix'),
(75, 'account'),
(75, 'profile'),
(75, 'project'),
(226, 'query')},
'macros.html.jinja2': {(220, 'path')},
'profile_layout.html.jinja2': {(4, 'profile'),
(167, 'calendarwidget_compact'),
(493, 'profile')}}
Jinja templates are a source of runtime errors because they have no static analysis of syntax or variable typing. This PR introduces a declarative syntax for template files and their context variables, allowing for static checks:
render_template
supplies all the expected variables (implemented withdataclass_transform
)@render_with(template_file_name, ...)