Add class-level customization and visibility control for TemplateHooks.

Review Request #14556 — Created Aug. 6, 2025 and submitted

Information

Djblets
release-5.x

Reviewers

TemplateHook has always required name, template_name, apply_to,
and extra_context to be passed into __init__(), even in a subclass.
This makes usage a bit verbose.

This change adds new class-level control of these options. Now, a
subclass can define these as class attributes and they'll be used if the
caller doesn't override any of them with a parameter.

Subclasses can also opt out of rendering by overriding
should_render(), which means they no longer need to handle this in a
template.

Unit tests pass.

Tested this with the banner component work in Review Board, utilizing
the new features.

Summary ID
Add class-level customization and visibility control for TemplateHooks.
`TemplateHook` has always required `name`, `template_name`, `apply_to`, and `extra_context` to be passed into `__init__()`, even in a subclass. This makes usage a bit verbose. This change adds new class-level control of these options. Now, a subclass can define these as class attributes and they'll be used if the caller doesn't override any of them with a parameter. Subclasses can also opt out of rendering by overriding `should_render()`, which means they no longer need to handle this in a template.
33373351585be57972d5c74fb9cee401d563f884
Description From Last Updated

Testing done?

daviddavid

Indentation doesn't match between TemplateHook and class MyTemplateHook

daviddavid

typo: facored -> factored

daviddavid

This list is missing the closing ]

maubinmaubin

We don't actually enforce that apply_to has been provided like we do with the name attribute. If apply_to hasn't been …

maubinmaubin

Same comment here, we're not actually enforcing that extra_content must be set or passed as an argument.

maubinmaubin

Given that this is typed as Mapping, we should probably assert isinstance(extra_context, Mapping)

daviddavid

Should be 5.3, not 7.1

daviddavid
david
  1. 
      
  2. Show all issues

    Testing done?

  3. djblets/extensions/hooks.py (Diff revision 1)
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    Show all issues

    Indentation doesn't match between TemplateHook and class MyTemplateHook

  4. djblets/extensions/hooks.py (Diff revision 1)
     
     
    Show all issues

    typo: facored -> factored

  5. 
      
chipx86
maubin
  1. 
      
  2. djblets/extensions/hooks.py (Diff revision 2)
     
     
    Show all issues

    This list is missing the closing ]

  3. djblets/extensions/hooks.py (Diff revision 2)
     
     
     
     
     
     
     
    Show all issues

    We don't actually enforce that apply_to has been provided like we do with the name attribute. If apply_to hasn't been passed as an argument and hasn't been set as an attribute we default it to an empty list.

    1. More of a docs issue. I'll fix that.

  4. djblets/extensions/hooks.py (Diff revision 2)
     
     
     
     
     
    Show all issues

    Same comment here, we're not actually enforcing that extra_content must be set or passed as an argument.

  5. 
      
chipx86
maubin
  1. Ship It!
  2. 
      
david
  1. 
      
  2. djblets/extensions/hooks.py (Diff revision 3)
     
     
    Show all issues

    Given that this is typed as Mapping, we should probably assert isinstance(extra_context, Mapping)

  3. djblets/extensions/hooks.py (Diff revision 3)
     
     
    Show all issues

    Should be 5.3, not 7.1

  4. 
      
chipx86
maubin
  1. Ship It!
  2. 
      
david
  1. Ship It!
  2. 
      
chipx86
Review request changed
Status:
Completed
Change Summary:
Pushed to release-5.x (65d55a1)