Procházet zdrojové kódy

Closes #16630: Enable plugins to embed custom <head> content (#19055)

Jeremy Stretch před 10 měsíci
rodič
revize
a00144026b

+ 1 - 0
docs/plugins/development/views.md

@@ -198,6 +198,7 @@ Plugins can inject custom content into certain areas of core NetBox views. This
 
 
 | Method              | View        | Description                                         |
 | Method              | View        | Description                                         |
 |---------------------|-------------|-----------------------------------------------------|
 |---------------------|-------------|-----------------------------------------------------|
+| `head()`            | All         | Custom HTML `<head>` block includes                 |
 | `navbar()`          | All         | Inject content inside the top navigation bar        |
 | `navbar()`          | All         | Inject content inside the top navigation bar        |
 | `list_buttons()`    | List view   | Add buttons to the top of the page                  |
 | `list_buttons()`    | List view   | Add buttons to the top of the page                  |
 | `buttons()`         | Object view | Add buttons to the top of the page                  |
 | `buttons()`         | Object view | Add buttons to the top of the page                  |

+ 7 - 0
netbox/netbox/plugins/templates.py

@@ -47,6 +47,13 @@ class PluginTemplateExtension:
     # Global methods
     # Global methods
     #
     #
 
 
+    def head(self):
+        """
+        HTML returned by this method will be inserted in the page's `<head>` block. This may be useful e.g. for
+        including additional Javascript or CSS resources.
+        """
+        raise NotImplementedError
+
     def navbar(self):
     def navbar(self):
         """
         """
         Content that will be rendered inside the top navigation menu. Content should be returned as an HTML
         Content that will be rendered inside the top navigation menu. Content should be returned as an HTML

+ 3 - 0
netbox/netbox/tests/dummy_plugin/template_content.py

@@ -3,6 +3,9 @@ from netbox.plugins.templates import PluginTemplateExtension
 
 
 class GlobalContent(PluginTemplateExtension):
 class GlobalContent(PluginTemplateExtension):
 
 
+    def head(self):
+        return "<!-- HEAD CONTENT -->"
+
     def navbar(self):
     def navbar(self):
         return "GLOBAL CONTENT - NAVBAR"
         return "GLOBAL CONTENT - NAVBAR"
 
 

+ 2 - 0
netbox/templates/base/base.html

@@ -3,6 +3,7 @@
 {% load helpers %}
 {% load helpers %}
 {% load i18n %}
 {% load i18n %}
 {% load django_htmx %}
 {% load django_htmx %}
+{% load plugins %}
 <!DOCTYPE html>
 <!DOCTYPE html>
 <html
 <html
   lang="en"
   lang="en"
@@ -59,6 +60,7 @@
 
 
     {# Additional <head> content #}
     {# Additional <head> content #}
     {% block head %}{% endblock %}
     {% block head %}{% endblock %}
+    {% plugin_head %}
 
 
   </head>
   </head>
   <body>
   <body>

+ 8 - 0
netbox/utilities/templatetags/plugins.py

@@ -45,6 +45,14 @@ def _get_registered_content(obj, method, template_context):
     return mark_safe(html)
     return mark_safe(html)
 
 
 
 
+@register.simple_tag(takes_context=True)
+def plugin_head(context):
+    """
+    Render any <head> content embedded by plugins
+    """
+    return _get_registered_content(None, 'head', context)
+
+
 @register.simple_tag(takes_context=True)
 @register.simple_tag(takes_context=True)
 def plugin_navbar(context):
 def plugin_navbar(context):
     """
     """