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

Introduce render_jinja2() convenience function

This commit is contained in:
Jeremy Stretch
2019-12-31 14:00:55 -05:00
parent f649b9f04f
commit 8a4293a4cc
3 changed files with 14 additions and 8 deletions

View File

@ -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)