# Template syntax ## Basics A template source (one `.json` file, or one entry of a dict of dicts) maps **keys** to lists of **alternatives**. When a key is expanded, its alternatives are joined with `|`: ```json { "abg": ["alpha", "beta", "gamma"], "$PATTERNS": ["I can match {{abg}}"] } ``` compiles to: ```text I can match (?Palpha|beta|gamma) ``` - Keys from *all* sources share one namespace: any template can reference any key, and defining the same key twice raises `DuplicatePatternKeyError`. - Only the entries under `$PATTERNS` become runnable patterns; the source's file stem (or dict key) becomes their match *type*. - Each expansion of a key gets a numbered group name: `abg_0`, `abg_1`, … Numbering is per runnable pattern, in order of expansion. - Alternatives can reference other keys; cycles are rejected at load time with `CircularReferenceError` naming the cycle path. ## Backreferences Use `{{#key}}` to re-match the text captured by the *previous* expansion of `key`, and `{{#key@n}}` to reach `n` expansions back: ```json { "abg": ["alpha", "beta", "gamma"], "$PATTERNS": ["{{abg}} and {{abg}}, again {{#abg}} and {{#abg@2}}"] } ``` ```text (?Palpha|beta|gamma) and (?Palpha|beta|gamma), again (?P=abg_1) and (?P=abg_0) ``` A backreference pointing before the first expansion raises `InvalidBackreferenceError` at load time. ## Unnamed and special groups Two equivalent ways to produce groups that don't capture by name: **Prefix the placeholder** — works for any special construct: | Placeholder | Expands to | |---|---| | `{{?:key}}` | `(?:…)` non-capturing | | `{{?>key}}` | `(?>…)` atomic | | `{{?=key}}` / `{{?!key}}` | lookahead / negative lookahead | | `{{?<=key}}` / `{{?