Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/plugins/development/views.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
7 changes: 7 additions & 0 deletions netbox/netbox/plugins/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def render(self, template_name, extra_context=None):
# 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
Expand Down
3 changes: 3 additions & 0 deletions netbox/netbox/tests/dummy_plugin/template_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

class GlobalContent(PluginTemplateExtension):

def head(self):
return "<!-- HEAD CONTENT -->"

def navbar(self):
return "GLOBAL CONTENT - NAVBAR"

Expand Down
2 changes: 2 additions & 0 deletions netbox/templates/base/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% load helpers %}
{% load i18n %}
{% load django_htmx %}
{% load plugins %}
<!DOCTYPE html>
<html
lang="en"
Expand Down Expand Up @@ -59,6 +60,7 @@

{# Additional <head> content #}
{% block head %}{% endblock %}
{% plugin_head %}

</head>
<body>
Expand Down
8 changes: 8 additions & 0 deletions netbox/utilities/templatetags/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down