Jinja2 Templates
Jinja2 Templates (DPJinja)
- STRING
Jinja2 Templates is the power-user end of the Dynamic Prompts pack. Where Random Prompts gives you {a|b|c} and wildcards, this node lets you write actual template logic - loops, conditionals, variables, random choices - using Jinja2, the same templating language that powers Flask and Ansible. If you've ever wanted your prompt to say "pick three colors, but only add 'glossy' when one of them is red," this is how you do it without hand-writing every case.
It's the node most people don't need and a few people can't live without. The bracket syntax covers 90% of prompt variety. Jinja is for the other 10% where you want real programmatic control over how the prompt gets built.
How it works
You write a Jinja2 template in the text box, and the node renders it into a finished prompt string every time it runs. Jinja gives you the toolbox you'd expect from a templating language - {% for %} loops to repeat sections, {% if %} conditionals to include or drop parts, variables to reuse a value, and random helpers to pick from lists. The Dynamic Prompts Jinja2 support adds prompt-oriented conveniences on top so you can, say, generate a randomized list of subjects and stitch them into one coherent prompt. The result is a single rendered string, exactly as if you'd typed it by hand - the diffusion model never knows a template was involved.
The practical use cases: building structured multi-part prompts where sections depend on each other, generating combinations that plain {|} syntax can't express cleanly, or reusing one value in several places in the prompt without repeating yourself. It's genuinely more capable than wildcards; it's also fiddlier, and a broken template throws an error instead of quietly doing nothing.
Inputs and outputs
Note this node is a little different from its siblings - there's no seed input. Just:
text- your Jinja2 template. Multiline, and you'll want the room.autorefresh-Yes/No.Norenders once and reuses the result;Yesre-renders every run, which is what you want if the template has random elements and you want them to actually vary.
Output is a STRING - the rendered prompt - wired into a CLIP Text Encode node, positive or negative.
Installing it
ComfyUI Manager → search dynamicprompts → install → restart. Or the manual route, which is the same as the rest of the pack:
git clone https://github.com/adieyal/comfyui-dynamicprompts custom_nodes/comfyui-dynamicprompts
python -m pip install -r custom_nodes/comfyui-dynamicprompts/requirements.txt
python custom_nodes/comfyui-dynamicprompts/install.py
mkdir custom_nodes/comfyui-dynamicprompts/wildcards
Restart after. Jinja support rides on the dynamicprompts library that requirements.txt installs, so don't skip that step.
Where people get burned
Template syntax errors are unforgiving. Wildcards fail soft - a missing file just prints as text. Jinja fails hard - a stray {% or a mismatched {% endfor %} throws, and your run stops. If the node errors, check your braces and tag pairs first; it's almost always an unclosed block or a typo in a tag name. Build the template up in small pieces rather than writing the whole thing and hoping.
It's easy to over-reach. The prompt-engineering reality is that most modern models don't reward a novel-length prompt - LLM-encoded bases drift past roughly 75–100 effective tokens, and even SDXL rewards a few strong concepts over a wall of text. Jinja makes it very easy to generate an enormous prompt just because you can. Use the power to make prompts smarter, not longer.
Debugging blind. As with the whole pack, the rendered prompt goes to the console unless you catch it. Wire the output into the OutputString node so you can actually see what your template produced on the canvas - with Jinja especially, you'll want that feedback loop while you're getting the logic right.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| text | STRING | — | |
| autorefresh | COMBO | 2 options: Yes, No |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| STRING | STRING | — |