Skip to content

Leverage Jinja Capabilities in sourcegen #232

@ischoegl

Description

@ischoegl

Abstract

Recent work on generated Clib and .NET APIs increased the use of Jinja2 templates. At the moment, the templates build on standard Jinja concepts, including loops and conditionals, but do not take advantage of Template Inheritance and Macros, among several others. The use of nested templates and macros has the potential to improve readability and facilitate long-term code maintenance.

Motivation

Describe the need for the proposed change:

  • What problem is it trying to solve? ... improve readability of templates.
  • Who is affected by the change? ... code maintainers.
  • Why is this a good solution? ... facilitate long-term maintenance.

Possible Solutions

While the current templates are somewhat convoluted, the resolution approach is straightforward. From the Python side, all nested template components need to be made available as dictionaries, where rendering would involve the following sketch:

jinja_templates = {
    # dictionary of templates
    'parent.j2': <...>,  # loaded from YAML as before
    'child.j2': <...>,  # loaded from YAML as before
}
env = Environment(loader=DictLoader(jinja_templates), **kwargs)
template = env.get_template('child.j2')
output = template.render(*args)

References

Jinja2 Documentation

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions