NLTRenderer
The main class for rendering NLT templates. Imported from pyrx_nlt.
Constructor
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
contact | dict[str, Any] | (required) | Contact data with flat fields and properties JSONB. Keys at root level are accessed in snake_case; keys in properties are accessed in original case then snake_case. |
trigger_event | dict[str, Any] | None | None | The event that triggered this template render. Must have event_name (str) and attributes (dict) keys. |
additional_events | dict[str, dict[str, Any]] | None | None | Additional named events, keyed by event name. Each value must have an attributes dict. |
event_store | EventStore | None | None | Optional async event lookup protocol for fetching events not in additional_events. |
template_loader | TemplateLoader | None | None | Optional template loader for {extends} template inheritance. |
Data Structure
render()
Render both subject and body templates. Resets suppression state before each call.
If any required expression in either the subject or body resolves to null, the entire result is suppressed -- both html and subject are set to None.
Parameters
| Parameter | Type | Description |
|---|---|---|
subject_template | str | NLT template string for the email subject line. |
body_template | str | NLT template string for the email body HTML. |
Returns
RenderResult -- see RenderResult below.
Example
render_text()
Render a single template string, resolving all AST nodes. Returns None if any required expression suppresses the send.
Parameters
| Parameter | Type | Description |
|---|---|---|
template | str | NLT template string to render. |
Returns
str | None -- the rendered text, or None if suppressed.
Example
resolve()
Resolve a single NLTExpression to its string value. Applies math, transforms, formatting, filters, and pluralization in order. Returns None if the expression has required=True and the value is missing. Sets self.suppressed as a side effect.
Resolution Order
- Resolve raw value from data source
- Handle null: check
required, apply fallback, return empty string - Apply math operation (if present)
- Convert to string
- Apply text transform (uppercase / lowercase / titlecase / capitalize)
- Apply format spec (currency / date / percentage)
- Apply filter chain (operates on raw value, re-resolved)
- Apply pluralization
Parameters
| Parameter | Type | Description |
|---|---|---|
expr | NLTExpression | A parsed NLT expression dataclass. |
Returns
str | None -- the resolved string value, or None if suppressed.
resolve_raw()
Resolve a source/attribute to a raw Python value (not stringified). Used internally by list, table, aggregate, for-loop, and condition nodes that need the original data type (list, dict, number) rather than a string.
Parameters
| Parameter | Type | Description |
|---|---|---|
source | str | Data source: "user", "trigger_event", or "named_event". |
attribute_path | str | The attribute name, e.g. "First Name", "Orders". Supports dot notation for nested access. |
event_name | str | None | Event name for named_event source. None for other sources. |
Returns
Any -- the raw Python value (dict, list, number, string, or None).
RenderResult
| Field | Type | Description |
|---|---|---|
html | str | None | The rendered HTML body. None if the send was suppressed. |
subject | str | None | The rendered subject line. None if the send was suppressed. |
suppressed | bool | True if any required expression resolved to null. Defaults to False. |
suppressed_reason | str | None | A human-readable description of why the send was suppressed, e.g. "Required attribute 'Order ID' is missing". |
unresolved_expressions | list[str] | List of {...} expression blocks that could not be parsed. These are left as literal text in the output. |
has_rendering_errors | bool | True if any expressions in the template could not be resolved. Convenience flag equivalent to len(unresolved_expressions) > 0. |
EventStore Protocol
Protocol for looking up the latest event of a given type for a contact. Used as a fallback when an event referenced in a template is not available in additional_events.
Currently reserved for future async integration. The synchronous rendering pipeline does not invoke async methods. Defined for forward compatibility.
Return Value
A dict with an attributes key containing the event data, or None if no matching event exists:
TemplateLoader Protocol
Protocol for loading base templates by name, used by the {extends} template inheritance feature.
Parameters
| Parameter | Type | Description |
|---|---|---|
template_name | str | The name of the template to load, as specified in {extends "name"}. |
Returns
str | None -- the raw template string, or None if the template is not found. If None is returned, the child template is rendered as-is.