Răsfoiți Sursa

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

Jeremy Stretch 10 luni în urmă
părinte
comite
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                                         |
 |---------------------|-------------|-----------------------------------------------------|
+| `head()`            | All         | Custom HTML `<head>` block includes                 |
 | `navbar()`          | All         | Inject content inside the top navigation bar        |
 | `list_buttons()`    | List 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
     #
 
+    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):
         """
         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):
 
+    def head(self):
+        return "<!-- HEAD CONTENT -->"
+
     def navbar(self):
         return "GLOBAL CONTENT - NAVBAR"
 

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

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

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

@@ -45,6 +45,14 @@ def _get_registered_content(obj, method, template_context):
     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)
 def plugin_navbar(context):
     """