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_type
variable to determine which message to display.{% when %}
: Defines specific responses based on the value oftask_type
.{% else %}
: Provides a default message for anytask_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
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.
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.