Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 3.7 — Prompt Templates as Code
LLM Engineering · Example 1 of 1

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.

01

Write render(template, values) that replaces {{name}}-style blanks in one pass and raises ValueError("missing variable: <name>") for a blank with no value.

02

Write variables_in(template) that returns the blank names in the order they first appear, without duplicates.

Explain it without notes

01

What problems does a prompt template solve compared with building strings by hand?

02

Explain the replace-in-a-loop bug and the fix.

03

Why should a template engine raise an error on a missing variable?

Naive replace vs a single-pass template filler
Sign in to run this example in your browser.

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