Skip to main content
All CollectionsPromptsWriting Prompts
[Advanced] Using Liquid Syntax for Branching Logic in Prompts
[Advanced] Using Liquid Syntax for Branching Logic in Prompts
Promptitude Team avatar
Written by Promptitude Team
Updated this week

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.


📌 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.

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?