Skip to content

Commit e96c382

Browse files
Merge pull request #9520 from kkthxbye-code/fix-9501
Fixes #9501 - Add configuration option JINJA2_FILTERS
2 parents 135ce93 + d1aa820 commit e96c382

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

docs/configuration/optional-settings.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,20 @@ HTTP_PROXIES = {
255255

256256
---
257257

258+
## JINJA2_FILTERS
259+
260+
Default: `{}`
261+
262+
A dictionary of custom jinja2 filters with the key being the filter name and the value being a callable. For more information see the [jinja2 documentation](https://jinja.palletsprojects.com/en/3.1.x/api/#custom-filters). For example:
263+
264+
```python
265+
JINJA2_FILTERS = {
266+
'uppercase': uppercase,
267+
}
268+
```
269+
270+
---
271+
258272
## INTERNAL_IPS
259273

260274
Default: `('127.0.0.1', '::1')`

netbox/netbox/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
FIELD_CHOICES = getattr(configuration, 'FIELD_CHOICES', {})
9797
HTTP_PROXIES = getattr(configuration, 'HTTP_PROXIES', None)
9898
INTERNAL_IPS = getattr(configuration, 'INTERNAL_IPS', ('127.0.0.1', '::1'))
99+
JINJA2_FILTERS = getattr(configuration, 'JINJA2_FILTERS', {})
99100
LOGGING = getattr(configuration, 'LOGGING', {})
100101
LOGIN_PERSISTENCE = getattr(configuration, 'LOGIN_PERSISTENCE', False)
101102
LOGIN_REQUIRED = getattr(configuration, 'LOGIN_REQUIRED', False)

netbox/utilities/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from dcim.choices import CableLengthUnitChoices
1515
from extras.plugins import PluginConfig
1616
from extras.utils import is_taggable
17+
from netbox.config import get_config
1718
from utilities.constants import HTTP_REQUEST_META_SAFE_COPY
1819

1920

@@ -257,7 +258,9 @@ def render_jinja2(template_code, context):
257258
"""
258259
Render a Jinja2 template with the provided context. Return the rendered content.
259260
"""
260-
return SandboxedEnvironment().from_string(source=template_code).render(**context)
261+
environment = SandboxedEnvironment()
262+
environment.filters.update(get_config().JINJA2_FILTERS)
263+
return environment.from_string(source=template_code).render(**context)
261264

262265

263266
def prepare_cloned_fields(instance):

0 commit comments

Comments
 (0)