# Nested Spintax How nesting transforms flat spinning into a powerful template engine — and why the Spintax.Net approach is the standard. ## What is Nested Spintax? Regular spintax picks a random option from a list: `{red|blue|green}` produces one of three colors. **Nested spintax** places spintax inside spintax — like a matryoshka doll, where each layer reveals more variation inside. When the engine encounters nested structures, it resolves from the **innermost expression outward**. The inner braces are evaluated first, and their result becomes part of the outer expression. ``` {red|{dark|light} blue} car ``` Here `{dark|light}` resolves first (e.g. `dark`), producing `{red|dark blue}`. Then the outer enumeration picks one option: `red` or `dark blue`. ## Why Nesting Matters Without nesting, your options are flat. Three enumerations with three options each give you 3 + 3 + 3 = 9 fragments. **With nesting**, the same elements combine multiplicatively: 3 × 3 × 3 = **27 unique variants** from a single compact template. This exponential growth is the key to generating truly unique content. A moderately complex template with nested enumerations and permutations can produce **thousands or millions** of distinct outputs — all from one carefully crafted source. ## Common Limitations of Other Tools Most spintax tools handle only the basics. Here is what typically goes wrong: - **Enum-in-enum only** — they support `{a|{b|c}}` but nothing else. No permutations, no variables, no includes inside other elements. - **Depth limits** — many parsers break after 2–3 levels of nesting, silently producing corrupted output. - **No cleanup** — after resolving nested structures, spacing collapses, punctuation doubles, capitalization breaks. The result needs manual editing. - **No composability** — without variables and includes, every template is an island. Reusing common blocks means copy-pasting. ## The Spintax.Net Approach Spintax.Net implements nesting as a first-class feature, not an afterthought. Five design decisions make it work: 1. **Arbitrary depth** — there is no nesting limit. Ten levels deep works the same as two. 2. **Cross-element nesting** — enumerations inside permutations, permutations inside enumerations, variables containing nested structures, includes embedding entire nested templates. Any element inside any other. 3. **Innermost-first resolution** — the engine always resolves from the deepest level outward. This makes evaluation predictable and debuggable. 4. **Smart post-processing** — after all nesting is resolved, the engine automatically fixes capitalization, collapses duplicate spaces, corrects punctuation spacing, and handles sentence boundaries. The output is clean text, not raw concatenation. 5. **Safety** — circular reference detection for `#include` prevents infinite loops. Variable scope rules (runtime > local > global) prevent accidental overwrites. ## From Simple to Advanced ### 1\. Enum inside enum ``` {{premium|luxury} sedan|{compact|mid-size} SUV} ``` Inner enumerations resolve first, then the outer one picks a result. Possible outputs: `premium sedan`, `luxury sedan`, `compact SUV`, `mid-size SUV`. ### 2\. Enum inside permutation ``` [ {red|blue} apples|{big|small} oranges|bananas] ``` Each permutation element contains its own enumeration. The engine resolves inner enumerations first, then shuffles and joins. Example output: `blue apples, bananas and small oranges`. ### 3\. `#set` variables with nested spintax ``` #set %product% = {{premium|budget} {laptop|tablet}|{smart|classic} phone} #set %action% = {Buy|Get|Order} %action% your new %product% today! ``` `#set` is a macro: it stores the nested spintax unresolved and re-rolls it at _every_ reference, so two `%product%` mentions may render two different products. Combined with multiple variables, the variant count multiplies rapidly. ### 4\. `#def` — roll once, hold everywhere ``` #def %brand% = {{Nord|Prime} Tools|Acme {Labs|Works}} %brand% ships today. Order %brand% now! ``` `#def` resolves its nested spintax **once per render** and holds that result at every reference — here both mentions name the same brand. Use `#def` when repeated references must agree (brand names, counts); use `#set` when every reference should vary on its own. ### 5\. Conditionals around nested blocks ``` {?PLAN?{Upgrade to|Unlock} {Pro|Premium} features|{Try|Start with} the free tier} ``` The `{?VAR?then|else}` pre-pass picks a branch by whether `%PLAN%` has a value — before any random picks run. The losing branch is discarded whole: nothing inside it is evaluated or rolled. ### 6\. Plurals with a nested count ``` #def %n% = {2|5|21} Renders %n% {plural %n%: variant|variants} per click. ``` The count is itself spintax — and its variable must be `#def`, not `#set`: the form is chosen after the count resolves, and a macro would still be unresolved spintax at that moment. The render locale decides how many forms you supply: two in English, three in Russian or Ukrainian. ### 7\. Includes with nesting ``` /# main template #/ #include "hero-text" {Check out|Discover|Explore} our [<, > features|plans|pricing]. ``` The included template can itself contain enumerations, permutations, variables, and even further includes. Circular reference detection keeps everything safe. ## AI + Nested Spintax Large language models are excellent at writing complex nested templates. A single prompt can produce a template with multiple nesting levels, value-driven conditional blocks (`{?VAR?…}`), locale-aware plurals, and reusable sections via includes. The workflow is simple: **use AI to create the template once, then use Spintax to generate unique variants cheaply forever**. One API call to create the template. Zero API calls to generate each variant. Nested spintax is what makes this economically viable — the deeper the nesting, the more unique outputs per template. ## Getting Started Ready to use nested spintax in your projects? Try it live in the [playground](https://spintax.net/play/) — paste any example above and watch it resolve. The [syntax reference](https://spintax.net/docs/syntax.md) has the complete specification, and when you are ready to render in production, pick one of the [open-source engines](https://spintax.net/spintax-engines.md) — or the [WordPress plugin](https://github.com/investblog/spintax) if that is your runtime. --- [Open in playground](https://spintax.net/play/) [Syntax Reference](https://spintax.net/docs/syntax.md) [All Docs](https://spintax.net/docs.md) --- Source: https://spintax.net/docs/nested-spintax/