-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Customably extend jinja 2 template namespace #13477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hello again, I looked a bit at the source code and understood the following.
For a class for example, the following snippet does most of the populating: sphinx/sphinx/ext/autosummary/generate.py Lines 396 to 415 in 021d6a8
So I think in the end, it would be very easy to add a # addition to `sphinx.sphinx.ext.autosummary.generate.generate_autosummary_content`
ns.update(_get_context_extension(
doc,
obj,
what,
config=config,
events=events,
registry=registry,
imported=imported_members,
) # exact signature to figure out
) that would call a user-defined function: # conf.py
def extend_template_content(doc, obj, what, options): # exact signature to figure out
"""Extend template content namespace with custom fields.
Example: only for dataclasses, add the `frozen` attribute to report class state.
"""
ext = {}
if what == "class" and is_dataclass(obj):
ext["frozen"] = obj.__dataclass_params__.frozen
return ext
def setup(app):
app.connect("extend-autosummary-template-content", extend_template_content) This simple user-side configuration would make the following template possible. .. _templates/dataclass.rst
Is this dataclass frozen?
{{ frozen }} What do you think about this feature? I think it would simplify so much customization! |
Uh oh!
There was an error while loading. Please reload this page.
Hello,
I'd like to propose a feature to extend the namespace available to jinja2 templates
Currently, the following are available (some only on classes or modules):
The proposed feature would allow custom extensions of this namespace, base on a
extend-autosummary-template-content
event. A minimal use case would be the following template... _templates/dataclass.rst Is this dataclass frozen? {{ frozen }}
More in the next message (this one was largely edited after further investigation from my side).
The text was updated successfully, but these errors were encountered: