Nodes/Sage Utils/Text Substitution
ComfyUI Node

Text Substitution

Template text with placeholders, filled in from wired inputs

By arcum42·Created 2 years ago·Updated 28 days ago· 33
Text Substitution
  • strings
  • result
text
delimiter$
prefix
suffix

If you've ever wanted a reusable prompt template - "a portrait of $str_1, wearing $str_2, in the style of $str_3" - and are tired of hand-building the string, this is the node. Text Substitution takes a template with placeholders, replaces those placeholders with whatever strings you wire in, and hands back the finished text. It's string templating, done as a node.

How it works

The text input holds your template with placeholders. Placeholders are the delimiter (default $) followed by str_1, str_2, str_3... - one placeholder per connected input slot, in order. The strings input is autogrow: start with one slot, plug in a string, and it grows to add str_2, str_3, and so on (up to 100). So with text = portrait of $str_1 in $str_2, and strings wired to "a red jacket" and "a rainstorm", you get portrait of a red jacket in a rainstorm.

The delimiter input lets you change what marks a placeholder - if your template text legitimately contains $ (say, prices), switch the delimiter to something like # and the $ stops being special. The optional prefix and suffix inputs get wrapped around the finished result.

One behavior worth knowing: it's forgiving. If a placeholder in the template has no matching input, the node leaves it as-is rather than erroring (a "safe substitution" fallback), so a template with extra placeholders still produces output - just with $str_4 sitting in it. And the mechanism underneath is Python's string.Template, which is why the $-delimiter behavior and the fallbacks feel the way they do.

When it's worth it

Any workflow that assembles text from parts - a prompt built from a subject slot, a style slot, a detail slot, where the pieces change independently. It's also a clean way to build a sentence template that several downstream nodes consume, or to format metadata strings with variables. If your pieces are just a fixed prefix and suffix, the plain Set Text node is simpler; once you have several variable slots, this one wins.

Installing it

Part of the Sage Utils pack - ComfyUI Manager (search Sage Utils) or:

cd ComfyUI/custom_nodes
git clone https://github.com/arcum42/ComfyUI_SageUtils
cd ComfyUI_SageUtils
pip install -r requirements.txt

Restart ComfyUI. Only pip dependency is dynamicprompts; no model downloads.

The gotchas

The placeholder names are positional - $str_1 is always the first connected slot, not a name you get to choose, so a template and its wiring must stay in sync. And remember a placeholder only counts if it's <delimiter>str_N exactly; a $thing without the str_ prefix won't be replaced. Missing-placeholder leftovers are silently kept, which is a feature until you forget one and spend a minute wondering why $str_2 is in your prompt.

CategorySage Utils/text/input

Inputs (5)

NameTypeDefaultDescription
textSTRINGThe template text containing placeholders.
delimiterSTRING$Delimiter used to mark placeholder variables.
stringsCOMFY_AUTOGROW_V3Input value for strings.
prefixoptSTRINGText to prepend to the substituted result.
suffixoptSTRINGText to append to the substituted result.

Outputs (1)

NameTypeDescription
resultSTRINGOutput value for result.