Liquid is a powerful templating language that enables dynamic content generation with branching logic. This guide will help you understand how to use Liquid's control structures to create dynamic and personalized prompts.
What is Liquid Syntax?
Liquid is a simple yet flexible templating language. It uses placeholders, filters, and tags to insert dynamic content into templates, making it popular for creating customized outputs. When integrated into prompts, liquid syntax allows for dynamic adjustments based on user input or context.
How to use Branching Logic in Prompts
Here’s how you can incorporate Liquid syntax into your prompts to make them more dynamic and personalized:
EXAMPLE:
1️⃣ Adding Conditional Statements
Use if, elsif, and else to display specific parts of a prompt based on conditions.
Explanation:
{{user_name}}: Displays the user’s name dynamically.{% if user_role == "admin" %}: Checks if the user role is "admin".{% if user_role == "editor" %}: Checks if the user role is "editor".{% else %}: Provides an alternative message if the role is not "admin" or “editor”.
When applied, this script personalizes messages based on the user's role, showing either "Admin!", "Editor!" or "Viewer!" depending on their assigned role.
2️⃣ Adding Case Statements
Simplify complex branching with case statements.
Explanation:
{{user_name}}: Dynamically inserts the user’s name into the greeting.{% case task_type %}: Evaluates thetask_typevariable to determine which message to display.{% when %}: Defines specific responses based on the value oftask_type.{% else %}: Provides a default message for anytask_typenot explicitly handled.
When applied, this script dynamically personalizes messages based on the task type, displaying "Review the submissions," "Approve or reject requests," or "Complete your task" depending on the value of task_type.
3️⃣ Loading Snippets Conditionally
Branching gets much more useful when the branches contain snippets instead of short sentences. Snippets are referenced with a dot namespace:
{{snippet.your-snippet-name}}
Liquid resolves the case before the prompt is sent, so only the matching branch is rendered. The snippets in the branches that don't match never reach the model — a large block of instruction text stays out of the prompt whenever its mode isn't selected.
Here's an example with three modes, each pulling in a different snippet:
{% assign mode = "repeat" %}
{% case mode %}
{% when "repeat" %}
{{snippet.repeat}}
{% when "investment" %}
{{snippet.investment}}
{% else %}
{{snippet.xyz}}
{% endcase %}
Explanation:
{% assign mode = "repeat" %}: sets the variable that decides the branch. You can also drive it from an input variable instead —{% case task_mode %}.{{snippet.repeat}}: inserts the full content of the snippet namedrepeat.{% else %}: the fallback snippet for any value you haven't listed.
Since mode is set to "repeat" here, the system message contains only the repeat snippet — the other two aren't there at all.
💡 Why this matters: the word count of a snippet counts towards the total input of your prompt. Conditional loading lets you keep several long instruction sets inside a single prompt without paying for all of them on every run.
📌 Best Practices for Prompts
Simplify Conditions: Keep conditions concise and readable to avoid confusing logic.
Test Outputs: Ensure that all branches generate the expected output under different scenarios.
Use Filters: Apply filters to format variables for better readability (e.g., capitalize). (Advanced: Filter Input Variables | Promptitude.io Help Center)
Focus on Clarity: Ensure the prompt remains clear and user-friendly, regardless of dynamic content.
Keep heavy instructions in snippets and switch between them with
case/wheninstead of maintaining one prompt per mode.
By incorporating Liquid syntax into your prompts, you can create dynamic, context-aware content that enhances user experience. Liquid provides the tools to make your prompts more engaging and effective.


