Nodes/Template Vars/Apply Template Variables
ComfyUI Node

Apply Template Variables

Render {character1} from variables you actually control

By boobkake22·Created 3 months ago·Updated 2 months ago· 0
Apply Template Variables
  • variables
  • text
templateThis is {character1} and {character2} standing in {location}.
placeholder_style{name}
missingkeep

This is the workhorse of the Template Vars pack. You feed it a template string with {placeholders} in it and a map of variables, and it hands you back a fully rendered string. Think "mail merge for prompts": one template, a bunch of named slots, and you swap the values without touching the sentence structure.

Where it pays off: character consistency across a batch. Your template is This is {character1} and {character2} standing in {location}. and you change only the three variables between runs - the model sees the same sentence every time, so the things you didn't change stay stable. That's the same move the community converged on for control via prompts, and it beats hand-editing a big prompt block every run. It also kills the copy-paste bug where one run keeps "a rainy train station" from the last time you edited the text box.

How it works

The variables come in as a TEMPLATE_VARS map (a plain dict under the hood) from the pack's creator nodes. Apply walks your template, finds each placeholder, and swaps in the matching value. Three knobs do the actual work:

  • placeholder_style - {name} (the default), {{name}}, $(name), or $name. Pick one and stay consistent. The double-brace and $( ) styles exist so your placeholders survive in text where single braces or $ mean something else.
  • missing - what happens when a variable isn't in the map: keep leaves the placeholder literally in the output, empty replaces it with nothing, error stops the run with a message. keep is the default and it's your friend while you're still wiring things up, because you can see exactly which slots never got filled.
  • template - the multiline text box with the placeholders in it.

One nice touch: the bracketed styles support inline defaults. {character1|someone} or $(location:an empty room) renders the fallback if the variable is missing. So you can ship a workflow where an unconnected character slot quietly degrades to "someone" instead of throwing - that's the keep and empty modes taken one step further.

Output is a single text (STRING). It's just a string - wire it into any regular CLIP Text Encode node, or any other text input in your graph. This node deliberately does not encode anything; if you want encode-and-go in one step, the pack's CLIP Text Encode With Template Variables does that (with stricter rules - see that article).

A trap worth knowing

The pack's text widgets explicitly disable ComfyUI's dynamic prompt expansion, because ComfyUI otherwise tries to interpret {...} as a wildcard or random-choice block and mangles your template. That's a feature, but it means the rules are stricter than ComfyUI's own text boxes: variable names must start with a letter or underscore and contain only letters, numbers, underscores, or hyphens, and values get whitespace-trimmed. If your variable is named char 1 it will error.

Installation

This is a dependency-free pack - no model downloads, no pip requirements. Install it once and it covers all six nodes:

  • ComfyUI Manager: search for "Template Vars" and install, then restart ComfyUI.
  • Manual: cd ComfyUI/custom_nodes && git clone https://github.com/boobkake22/ComfyUI-TemplateVars, then restart.

The nodes live under the Template Vars category. If you're on an older ComfyUI build, the pack falls back to a V1 implementation - Apply itself looks the same, but the merge/from-strings nodes lose their dynamic inputs and show fixed slots instead.

CategoryTemplate Vars

Inputs (4)

NameTypeDefaultDescription
templateSTRINGThis is {character1} and {character2} standing in {location}.
placeholder_styleCOMBO{name}4 options: {name}, {{name}}, $(name), $name
missingCOMBOkeep3 options: keep, empty, error
variablesoptTEMPLATE_VARS

Outputs (1)

NameTypeDescription
textSTRING