Converter
Bidirectional template conversion between NLT and Jinja2 syntax. All functions are in pyrx_nlt.converter.
convert_jinja2_to_nlt()
Convert a Jinja2 template to NLT syntax. Handles UserAttribute['...'], EventAttribute['...'], control flow ({% if %}, {% for %}), and filters.
Also recovers any NLT expressions that were previously preserved in Jinja2 comments ({# NLT: {...} #}`) during a prior NLT-to-Jinja2 conversion.
Parameters
| Parameter | Type | Description |
|---|---|---|
template | str | The Jinja2 template to convert. |
Returns
tuple[str, ConversionResult] -- the converted template string and conversion metadata.
Example
Supported Conversions
Variables:
{{ UserAttribute['Field'] }}with all modifier variants (default, upper, lower, title, truncate, round, length, first, last, sort, join, replace){{ EventAttribute['event.Field'] }}with default and required variantsMOE_NOT_SENDdefaults are converted to therequiredmodifier
Control flow:
{% if UserAttribute['X'] == 'Y' %}/{% elif %}/{% else %}/{% endif %}{% if EventAttribute['X.Y'] == 'Z' %}{% if UserAttribute['X'] is defined %}(existence check){% for item in UserAttribute['X'] %}/{% endfor %}{% set x = expr %}{% raw %}...{% endraw %}
convert_nlt_to_jinja2()
Convert an NLT template to Jinja2 syntax.
NLT-only constructs ({sum}, {average}, {count}, {list}, {table}) have no Jinja2 equivalent. They are preserved in Jinja2 comments as {# NLT: {original} #}` so round-trip conversions can recover them.
Parameters
| Parameter | Type | Description |
|---|---|---|
template | str | The NLT template to convert. |
Returns
tuple[str, ConversionResult] -- the converted template string and conversion metadata. The warnings field will contain messages about any NLT-only constructs that were preserved as comments.
Example
Supported Conversions
Variables:
{the user's Field}with all modifier variants (required, or, uppercase, lowercase, titlecase, truncate, round, length, first, last, sort, join, replace){the Field from the trigger event}with modifier variants{the Field from the EventName event}with modifier variants
Control flow:
{if the user's X is "Y"}/{else if}/{else}/{end if}{if the user's X is set}{for item in the user's X}/{end for}{set x to expr}{item.property}/{loop.index}
NLT-only (preserved as comments):
{sum of ...},{average of ...},{count of ...}{list ...},{table ...}
detect_template_language()
Detect whether a template uses NLT syntax, Jinja2 syntax, both, or neither.
Parameters
| Parameter | Type | Description |
|---|---|---|
template | str | The template to analyze. |
Returns
| Return Value | Meaning |
|---|---|
"nlt" | Template contains only NLT expressions. |
"jinja2" | Template contains only Jinja2 expressions ({{ }} or {% %}). |
"mixed" | Template contains both NLT and Jinja2 expressions. |
"none" | Template contains no dynamic expressions (plain HTML/text). |
Example
Use this function in migration pipelines to skip templates that are already in the target format and flag "mixed" templates for manual review.
count_expressions()
Count NLT and Jinja2 expressions in a template.
Parameters
| Parameter | Type | Description |
|---|---|---|
template | str | The template to analyze. |
Returns
tuple[int, int] -- (nlt_count, jinja2_count).
Example
ConversionResult
Metadata returned alongside every conversion.
| Field | Type | Description |
|---|---|---|
count | int | Number of expressions successfully converted. |
warnings | list[str] | Conversion warnings, e.g. NLT-only constructs preserved as comments. |
unhandled | list[str] | Expressions that could not be converted and were left unchanged. |
source_language | Literal["nlt", "jinja2", "mixed", "none"] | The detected language of the input template. |
target_language | Literal["nlt", "jinja2"] | The language the template was converted to. |