Skip to content

Commit a001440

Browse files
Closes #16630: Enable plugins to embed custom <head> content (#19055)
1 parent 8d7889e commit a001440

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed

docs/plugins/development/views.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ Plugins can inject custom content into certain areas of core NetBox views. This
198198

199199
| Method | View | Description |
200200
|---------------------|-------------|-----------------------------------------------------|
201+
| `head()` | All | Custom HTML `<head>` block includes |
201202
| `navbar()` | All | Inject content inside the top navigation bar |
202203
| `list_buttons()` | List view | Add buttons to the top of the page |
203204
| `buttons()` | Object view | Add buttons to the top of the page |

netbox/netbox/plugins/templates.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ def render(self, template_name, extra_context=None):
4747
# Global methods
4848
#
4949

50+
def head(self):
51+
"""
52+
HTML returned by this method will be inserted in the page's `<head>` block. This may be useful e.g. for
53+
including additional Javascript or CSS resources.
54+
"""
55+
raise NotImplementedError
56+
5057
def navbar(self):
5158
"""
5259
Content that will be rendered inside the top navigation menu. Content should be returned as an HTML

netbox/netbox/tests/dummy_plugin/template_content.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
class GlobalContent(PluginTemplateExtension):
55

6+
def head(self):
7+
return "<!-- HEAD CONTENT -->"
8+
69
def navbar(self):
710
return "GLOBAL CONTENT - NAVBAR"
811

netbox/templates/base/base.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{% load helpers %}
44
{% load i18n %}
55
{% load django_htmx %}
6+
{% load plugins %}
67
<!DOCTYPE html>
78
<html
89
lang="en"
@@ -59,6 +60,7 @@
5960

6061
{# Additional <head> content #}
6162
{% block head %}{% endblock %}
63+
{% plugin_head %}
6264

6365
</head>
6466
<body>

netbox/utilities/templatetags/plugins.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ def _get_registered_content(obj, method, template_context):
4545
return mark_safe(html)
4646

4747

48+
@register.simple_tag(takes_context=True)
49+
def plugin_head(context):
50+
"""
51+
Render any <head> content embedded by plugins
52+
"""
53+
return _get_registered_content(None, 'head', context)
54+
55+
4856
@register.simple_tag(takes_context=True)
4957
def plugin_navbar(context):
5058
"""

0 commit comments

Comments
 (0)