Skip to main content

[Advanced] Using Liquid Syntax for Branching Logic in Prompts

Written by Promptitude Team

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:

  1. {{user_name}}: Displays the user’s name dynamically.

  2. {% if user_role == "admin" %}: Checks if the user role is "admin".

  3. {% if user_role == "editor" %}: Checks if the user role is "editor".

  4. {% 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:

  1. {{user_name}}: Dynamically inserts the user’s name into the greeting.

  2. {% case task_type %}: Evaluates the task_type variable to determine which message to display.

  3. {% when %}: Defines specific responses based on the value of task_type.

  4. {% else %}: Provides a default message for any task_type not 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:

  1. {% assign mode = "repeat" %}: sets the variable that decides the branch. You can also drive it from an input variable instead — {% case task_mode %}.

  2. {{snippet.repeat}}: inserts the full content of the snippet named repeat.

  3. {% 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

  1. Simplify Conditions: Keep conditions concise and readable to avoid confusing logic.

  2. Test Outputs: Ensure that all branches generate the expected output under different scenarios.

  3. Use Filters: Apply filters to format variables for better readability (e.g., capitalize). (Advanced: Filter Input Variables | Promptitude.io Help Center)

  4. Focus on Clarity: Ensure the prompt remains clear and user-friendly, regardless of dynamic content.

  5. Keep heavy instructions in snippets and switch between them with case/when instead 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.

Did this answer your question?