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

This commit is contained in:
Jeremy Stretch 2025-04-01 15:09:49 -04:00 committed by GitHub
parent 8d7889e2c0
commit a00144026b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 21 additions and 0 deletions

View File

@ -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 |

View File

@ -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

View File

@ -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"

View File

@ -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>

View File

@ -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):
""" """