Skip to main content

Natural Language Template engine — plain-English templates for email, notifications, and dynamic content

Project description

pyrx-nlt

Natural Language Template (NLT) Engine — Plain-English templates that non-technical teams can read, write, and maintain for email, notifications, and dynamic content.

Installation

pip install pyrx-nlt

Requires Python 3.10+. Zero third-party dependencies.

Quick Start

from pyrx_nlt import NLTRenderer

renderer = NLTRenderer(
    contact={"first_name": "Alice", "properties": {"Plan": "premium", "Points": 1250}},
    trigger_event={"event_name": "purchase", "attributes": {"Item": "Widget", "Amount": "49.99"}},
)

result = renderer.render(
    subject_template='Order confirmation for {the user\'s First Name, or "Customer"}',
    body_template="""
        <h1>Thank you, {the user's First Name}!</h1>
        <p>You purchased: {the Item from the trigger event}</p>
        <p>Amount: {the Amount from the trigger event, as "currency"}</p>
        {if the user's Plan is premium}
            <p>You earned {the user's Points, as "currency"} in rewards!</p>
        {else}
            <p>Upgrade to Premium for rewards on every purchase.</p>
        {end if}
    """,
)

print(result.html)      # Rendered HTML
print(result.subject)   # "Order confirmation for Alice"
print(result.suppressed) # False

Features

  • 3 data sources: {the user's X}, {the X from the trigger event}, {the X from the Y event}
  • Modifiers: or "fallback", required, as "format", uppercase/lowercase/titlecase
  • Math: plus, minus, times, divided by, modulo
  • Conditionals: {if}...{else if}...{else}...{end if} with 20 comparison operators, AND/OR/NOT
  • Loops: {for item in source}...{end for} with loop.index, loop.first, loop.last
  • Lists: {list source, as comma/ordered/unordered list}
  • Tables: {table source with columns: A, B, C}
  • Aggregates: {sum of}, {average of}, {count of}
  • 19 chainable filters: truncate, replace, url_encode, round, default, strip_html, trim, newline_to_br, split, join, first, last, length, sort, reverse, unique, where, map, slice
  • Formatting: 17 currency codes, date presets, custom date tokens, percentages
  • Variable assignment: {set x to expression}
  • Ternary: {condition ? "true" : "false"}
  • Comments: {# ignored #}
  • Raw blocks: {raw}...{end raw}
  • Macros: {define name(params)}...{end define} / {use name(args)}
  • Template inheritance: {extends "base.html"} / {block name}...{end block} / {parent}
  • Built-in variables: {today}, {now}
  • XSS-safe: All output HTML-escaped by default
  • Graceful degradation: Unparseable expressions preserved as literal text

Extension Points

from pyrx_nlt import NLTRenderer, EventStore, TemplateLoader

class MyEventStore:
    """Implement to look up historical events for named event resolution."""
    async def get_latest(self, contact_id: str, event_name: str) -> dict | None:
        ...

class MyTemplateLoader:
    """Implement for template inheritance ({extends "base.html"})."""
    def load(self, template_name: str) -> str | None:
        ...

renderer = NLTRenderer(
    contact=contact_data,
    event_store=MyEventStore(),
    template_loader=MyTemplateLoader(),
)

Jinja2 Migration

Built-in bidirectional converter for migrating from Jinja2/MoEngage templates:

from pyrx_nlt.converter import convert_jinja2_to_nlt, convert_nlt_to_jinja2, detect_template_language

nlt_template, result = convert_jinja2_to_nlt(jinja2_template)
print(f"Converted {result.count} expressions")

language = detect_template_language(template)  # "nlt", "jinja2", "mixed", or "none"

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyrx_nlt-1.0.0.tar.gz (96.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyrx_nlt-1.0.0-py3-none-any.whl (38.8 kB view details)

Uploaded Python 3

File details

Details for the file pyrx_nlt-1.0.0.tar.gz.

File metadata

  • Download URL: pyrx_nlt-1.0.0.tar.gz
  • Upload date:
  • Size: 96.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for pyrx_nlt-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e7cc115559f9f24e99f97498dd781446419f2395b4fc17bafd0b6f0492854bee
MD5 0bcbcfc809c7ddbb006674b238dce9df
BLAKE2b-256 1d7061321e04641f5cf64b5eb23d4f4b8233f2c1629b4fae0a67de625a2c1e7b

See more details on using hashes here.

File details

Details for the file pyrx_nlt-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyrx_nlt-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 38.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for pyrx_nlt-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8fdcb2891e7a14056e4f0615d668d44fbd9be5e0d9fe6e25da5a4ba2d3866cb5
MD5 5f7d4a8411d9c3ee94f71b47d50cf5a0
BLAKE2b-256 df3cff1650eb60ed95203626e2c42aead5b94a43c519f3dc28ebe6b7ba77bd93

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page