mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-22 20:12:00 -06:00
Introduce render_jinja2() convenience function
This commit is contained in:
parent
f649b9f04f
commit
8a4293a4cc
@ -12,12 +12,11 @@ from django.db.models import F, Q
|
||||
from django.http import HttpResponse
|
||||
from django.template import Template, Context
|
||||
from django.urls import reverse
|
||||
from jinja2 import Environment
|
||||
from taggit.models import TagBase, GenericTaggedItemBase
|
||||
|
||||
from dcim.constants import CONNECTION_STATUS_CONNECTED
|
||||
from utilities.fields import ColorField
|
||||
from utilities.utils import deepmerge, foreground_color, model_names_to_filter_dict
|
||||
from utilities.utils import deepmerge, foreground_color, model_names_to_filter_dict, render_jinja2
|
||||
from .constants import *
|
||||
from .querysets import ConfigContextQuerySet
|
||||
|
||||
@ -502,8 +501,7 @@ class ExportTemplate(models.Model):
|
||||
output = template.render(Context(context))
|
||||
|
||||
elif self.template_language == TEMPLATE_LANGUAGE_JINJA2:
|
||||
template = Environment().from_string(source=self.template_code)
|
||||
output = template.render(**context)
|
||||
output = render_jinja2(self.template_code, context)
|
||||
|
||||
else:
|
||||
return None
|
||||
|
@ -3,9 +3,9 @@ from collections import OrderedDict
|
||||
from django import template
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.utils.safestring import mark_safe
|
||||
from jinja2 import Environment
|
||||
|
||||
from extras.models import CustomLink
|
||||
from utilities.utils import render_jinja2
|
||||
|
||||
|
||||
register = template.Library()
|
||||
@ -46,7 +46,7 @@ def custom_links(obj):
|
||||
|
||||
# Add non-grouped links
|
||||
else:
|
||||
text_rendered = Environment().from_string(source=cl.text).render(**context)
|
||||
text_rendered = render_jinja2(cl.text, context)
|
||||
if text_rendered:
|
||||
link_target = ' target="_blank"' if cl.new_window else ''
|
||||
template_code += LINK_BUTTON.format(
|
||||
@ -59,7 +59,7 @@ def custom_links(obj):
|
||||
links_rendered = []
|
||||
|
||||
for cl in links:
|
||||
text_rendered = Environment().from_string(source=cl.text).render(**context)
|
||||
text_rendered = render_jinja2(cl.text, context)
|
||||
if text_rendered:
|
||||
link_target = ' target="_blank"' if cl.new_window else ''
|
||||
links_rendered.append(
|
||||
@ -72,6 +72,6 @@ def custom_links(obj):
|
||||
)
|
||||
|
||||
# Render template
|
||||
rendered = Environment().from_string(source=template_code).render(**context)
|
||||
rendered = render_jinja2(template_code, context)
|
||||
|
||||
return mark_safe(rendered)
|
||||
|
@ -4,6 +4,7 @@ from collections import OrderedDict
|
||||
|
||||
from django.core.serializers import serialize
|
||||
from django.db.models import Count, OuterRef, Subquery
|
||||
from jinja2 import Environment
|
||||
|
||||
from dcim.constants import LENGTH_UNIT_CENTIMETER, LENGTH_UNIT_FOOT, LENGTH_UNIT_INCH, LENGTH_UNIT_METER
|
||||
|
||||
@ -174,3 +175,10 @@ def to_meters(length, unit):
|
||||
if unit == LENGTH_UNIT_INCH:
|
||||
return length * 0.3048 * 12
|
||||
raise ValueError("Unknown unit {}. Must be 'm', 'cm', 'ft', or 'in'.".format(unit))
|
||||
|
||||
|
||||
def render_jinja2(template_code, context):
|
||||
"""
|
||||
Render a Jinja2 template with the provided context. Return the rendered content.
|
||||
"""
|
||||
return Environment().from_string(source=template_code).render(**context)
|
||||
|
Loading…
Reference in New Issue
Block a user