Naive replace vs a single-pass template filler
The secret is a harmless stand-in string. The point is the structure: a value must never act as a new placeholder.
A prompt template is text with named blanks, filled by a function. A real template engine fills each blank once, complains about missing values, and never lets an inserted value act as a new blank.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write render(template, values) that replaces {{name}}-style blanks in one pass and raises ValueError("missing variable: <name>") for a blank with no value.
Write variables_in(template) that returns the blank names in the order they first appear, without duplicates.
Explain it without notes
What problems does a prompt template solve compared with building strings by hand?
Explain the replace-in-a-loop bug and the fix.
Why should a template engine raise an error on a missing variable?
Expected output
naive: Reply to Ana KEY-123 (stand-in secret) about order A-17.
safe: Reply to Ana {{api_key}} about order A-17.
error: missing variable: name