Wiki Logic: Making a Document Change Itself
A wiki with variables produces the same document every time, with different words filled in. A wiki with logic produces a different document depending on what the form said – clauses that appear only when they apply, sections that repeat once per person, numbers worked out for you.
This article is the map. It explains the three tools, when to reach for each, and – importantly – when to use logic rather than an AI instruction.
Table of Contents
What Logic Is For
Consider a will. The same template has to serve someone with three children and someone with none, someone married in community of property and someone single, an estate split equally and an estate split by percentage.
Without logic you have two bad options: maintain a separate template for every combination, or leave clauses in that do not apply and hope nobody notices. Logic gives you a third: one template that decides for itself.
Note: Logic runs before the document is produced, against the answers on the submitted form. It is not something the reader interacts with – by the time they see the PDF, the decisions have already been made.
The Three Tools
| Tool | Question it answers | Looks like |
|---|---|---|
| Conditions | Should this text appear at all? | {IF:has_children}…{/IF} |
| Loops | How do I say this once per row? | {EACH:heirs}…{/EACH} |
| Calculated fields | What is the answer to a sum or a test? | AGE({.id_number}) < 18 |
They stack. A calculated field works out whether a beneficiary is a minor; a condition uses that answer to include a trust clause; a loop repeats the clause for each minor. Most real templates use all three.
1. Conditions – show or hide
A condition wraps a piece of the document and includes it only when something is true.
{IF:marital_status="Married"}
My spouse, {spouse_name}, shall inherit the residue of my estate.
{/IF}Full syntax, including {ELSE}, comparisons and combining tests: Show or Hide Parts of a Document.
2. Loops – repeat per row
When a form collects a list – beneficiaries, assets, guardians – a loop writes the same sentence once for each row, substituting that row's values.
{EACH:beneficiaries}
I bequeath {.share} of my estate to {.full_name}.
{/EACH}Numbering, first-and-last handling, nesting and referring to the previous row: Repeating a Section for Every Row.
3. Calculated fields – work out the answer
Some decisions need arithmetic or a test that the form did not ask directly. "Is this person under 18?" is not a form question – it is a calculation from their date of birth. You define it once in the Calc tab of the wiki editor and then use it like any other variable.
is_minor = AGE({.id_or_dob}) < 18Scope, the entity rule, and the full function list: Calculated Fields in Wikis.
Logic or AI?
Wikis also support AI instructions ({AI: …}), and it is tempting to describe a rule in plain English and let the AI apply it. For anything with a definite answer, prefer logic.
| Logic | AI instruction | |
|---|---|---|
| Same input, same output? | Always | Not guaranteed |
| Can you test it? | Yes – Preview shows exactly what happened | Only by reading the result |
| Cost per document | None | An AI call |
| Good at | Rules with a definite answer | Tone, phrasing, summarising, rewriting prose |
The test is simple: could you write the rule as a table? If yes, it is logic.
| You want | Reach for |
|---|---|
| Remove the guardianship section when there are no minor children | {IF:…} |
| List every beneficiary with their share | {EACH:…} |
| Use "he" or "she" based on a gender field | {IF:…} – two fixed options |
| Split the estate equally unless percentages were given | Calculated field + {IF:…} |
| Rewrite a client's rambling free-text wish into formal legal phrasing | {AI: …} |
| Summarise a long list of assets into a readable paragraph | {AI: …} |
Important: In documents with legal effect, an AI instruction that "usually" produces the right clause is a risk you do not need to take. If the rule is definite, encode it as logic – then the same form always produces the same document, and you can prove it in Preview.
A Worked Example
The guardianship section of a will: include it only when there is at least one child under 18, and name each of them.
Step 1 – a calculated field on the children list, scope per row, named is_minor:
AGE({.id_or_dob}) < 18Step 2 – a second calculated field, scope per doc, named minor_count:
COUNTIF({children}, {.is_minor})Step 3 – the wiki body:
{IF:minor_count>0}
{#clause:guardian} APPOINTMENT OF GUARDIAN
I appoint {guardian_name} as guardian of my minor children, namely
{EACH:children}{IF:.is_minor}{.full_name}{/IF}{/EACH}.
{/IF}Someone with no minor children gets no guardianship clause, no empty heading and no gap in the clause numbering. Someone with two gets both names. Nobody had to maintain two templates.
Where to Go Next
- Show or Hide Parts of a Document – conditions in full
- Repeating a Section for Every Row – loops in full
- Calculated Fields in Wikis – formulas and the function list
- Numbering Clauses and Cross-Referencing Them – automatic numbering that survives dropped clauses
- Working with Wikis – the editor, headers and footers, PDF output
Tip: Build logic in small steps and use the Preview button in the wiki editor after each one. Preview renders the wiki against a real submission and shows which branches were taken, which is far faster than generating a PDF and reading it.