feat: add component hooks (#631)

This commit is contained in:
Juro Oravec 2024-08-31 13:38:28 +02:00 committed by GitHub
parent 3c6f478f8a
commit 0cfc40231b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 253 additions and 0 deletions

View file

@ -169,6 +169,27 @@ class Component(Generic[ArgsType, KwargsType, DataType, SlotsType], metaclass=Co
View = ComponentView
def on_render_before(self, context: Context, template: Template) -> None:
"""
Hook that runs just before the component's template is rendered.
You can use this hook to access or modify the context or the template.
"""
pass
def on_render_after(self, context: Context, template: Template, content: str) -> Optional[SlotResult]:
"""
Hook that runs just after the component's template was rendered.
It receives the rendered output as the last argument.
You can use this hook to access the context or the template, but modifying
them won't have any effect.
To override the content that gets rendered, you can return a string or SafeString
from this hook.
"""
pass
def __init__(
self,
registered_name: Optional[str] = None,
@ -563,8 +584,13 @@ class Component(Generic[ArgsType, KwargsType, DataType, SlotsType], metaclass=Co
},
}
):
self.on_render_before(context, template)
rendered_component = template.render(context)
new_output = self.on_render_after(context, template, rendered_component)
rendered_component = new_output if new_output is not None else rendered_component
if is_dependency_middleware_active():
output = RENDERED_COMMENT_TEMPLATE.format(name=self.name) + rendered_component
else: