1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Move utilities.utils.render_jinja2() to utilities.jinja2

This commit is contained in:
Jeremy Stretch
2024-03-21 13:21:45 -04:00
parent c30d22335a
commit 1d3efc90c0
3 changed files with 18 additions and 12 deletions

View File

@ -1,6 +1,9 @@
from django.apps import apps
from jinja2 import BaseLoader, TemplateNotFound
from jinja2.meta import find_referenced_templates
from jinja2.sandbox import SandboxedEnvironment
from netbox.config import get_config
__all__ = (
'DataFileLoader',
@ -35,3 +38,16 @@ class DataFileLoader(BaseLoader):
def cache_templates(self, templates):
self._template_cache.update(templates)
#
# Utility functions
#
def render_jinja2(template_code, context):
"""
Render a Jinja2 template with the provided context. Return the rendered content.
"""
environment = SandboxedEnvironment()
environment.filters.update(get_config().JINJA2_FILTERS)
return environment.from_string(source=template_code).render(**context)