
Introduction
In Langtail, templating with Handlebars helps you build smarter, dynamic prompts for LLM apps. It’s straightforward: templates let your prompts change based on the data they get. This means you can handle complex cases without writing tons of code.Why it’s useful
1
Dynamic Content:
Use variables to personalize prompts based on user input or context.
2
Conditional Content:
Include or exclude parts of your prompts depending on the situation.
3
Prompt Variations:
Quickly test different ways of asking or responding without redoing your
work.
Setting Up Your Environment
1
Log into Langtail and select your project.
2
Navigate to 'Prompts' to either open an existing prompt or create a new one.
3
In the 'Template' section, craft your prompt.
Utilize Handlebars syntax and variables for dynamic content creation.
Example

Simple Templating with Variables
Use variables to customize prompts based on user input or context, making your interactions with GPT models more dynamic.- Inserting Variables:
- Syntax:
{{variableName}}
- Example: To customize a chatbot response, you might use:
- Syntax:
Example
Date helper
You can use the$date
helper to print the current date in the format MMMM dd, yyyy
:
Example
Example
dateVar
variable is set to 2024-09-01
, this example will print just “09”:
Example
Using Conditionals in Templates
Conditionals in your templates allow for dynamic changes in the prompts based on specific input data you send to the LLM. This enables you to tailor the interaction based on predefined conditions.- Basic Conditionals:
- Syntax:
{{#if condition}}...{{/if}}
- Example: If you’re creating a prompt where the response varies based on the time of day provided as input, you might use:
- Syntax:
Example
hour
is a variable that you would set based on the user’s
local time before sending the prompt to the LLM.
Section 4: Advanced Conditionals with Helpers
For more complex logic within your Langtail prompts, you can use Handlebars helpers. These helpers allow you to perform operations like comparison, logical AND/OR, and more, enabling sophisticated control over the content of your prompts based on the input data.Using Helpers
Helpers likeeq
, ne
, lt
, gt
, lte
, gte
, and
, or
can be used to evaluate conditions. This enables you to create dynamic prompts that respond to specific input data.
- Syntax for Helpers:
{{#if (helperName arg1 arg2)}}...{{/if}}
- Helpers are used within the
{{#if ...}}
block to evaluate conditions.
Examples
Equality Check (eq)
- Purpose: Check if two values are equal.
- Example: Customize prompt based on user subscription status.
Example
Greater Than (gt)
- Purpose: Check if one value is greater than another.
- Example: Offer content based on user’s score.
Example
Logical AND (and)
- Purpose: Check if multiple conditions are true.
- Example: Provide options based on user’s age and consent.
Example
Logical OR (or)
- Purpose: Check if at least one of multiple conditions is true.
- Example: Adjust prompt based on user’s role.
Example
Handling Newlines in Templates
When working with templates in Langtail, it’s important to manage newlines carefully to ensure they don’t negatively impact the LLM’s response quality. To address this, Langtail automatically adjusts newlines in specific cases within your templates. This adjustment applies to structures likeif
, else
, each
, unless
, and with
. Here’s how it works:
- First Newline After Opening Tags: The first newline immediately following an opening tag (
{{#if}}
,{{#each}}
,{{#unless}}
,{{#with}}
) is automatically removed. This prevents unintended breaks that could confuse the LLM. - Newlines Around
{{else}}
: Newlines immediately before and after{{else}}
are removed. This ensures that the content before and after the{{else}}
is treated as a continuous block without unnecessary breaks. - Newlines Before and After Closing Tags: Newlines immediately before and after closing tags (
{{/if}}
,{{/each}}
,{{/unless}}
,{{/with}}
) are also removed. This action keeps the template compact and avoids introducing breaks that the LLM might misinterpret.
Example
For example this template when hour is less than 12:Example
{{#if}}
tag as follows:
Example