Conditionals Guide¶
Conditionals let you show or hide content in your document based on data values. They're perfect for creating flexible templates that adapt to different scenarios.
Basic Conditional Syntax¶
Simple If Statement¶
When content is shown:
- Variable exists and is true
- Variable exists and is not empty/zero/false
JSON:
Template:
Dear Customer,
{{#if IsVIP}}
Thank you for being a VIP member! You get 20% off today.
{{/if}}
Best regards
Output (when IsVIP is true):
If-Else Statement¶
JSON:
Template:
{{#if IsPremium}}
Welcome, Premium Member! Enjoy unlimited access.
{{#else}}
Upgrade to Premium to unlock all features.
{{/if}}
Output:
If-ElseIf-Else Statement¶
Use {{#elseif}} to check multiple conditions in sequence:
{{#if Condition1}}
Content for condition 1
{{#elseif Condition2}}
Content for condition 2
{{#elseif Condition3}}
Content for condition 3
{{#else}}
Default content
{{/if}}
JSON:
Template:
{{#if Score >= 90}}
Grade: A - Excellent!
{{#elseif Score >= 80}}
Grade: B - Good
{{#elseif Score >= 70}}
Grade: C - Satisfactory
{{#elseif Score >= 60}}
Grade: D - Needs Improvement
{{#else}}
Grade: F - Please see instructor
{{/if}}
Output:
How it works:
- Conditions are evaluated in order, from top to bottom
- The first condition that is true wins - its content is shown
- All other branches are removed
- The {{#else}} branch is optional and acts as a fallback
- Important: {{#else}} must always be the last branch
Benefits over nested conditionals: - Cleaner, more readable templates - Easier to add/remove conditions - Less indentation and fewer closing tags
Comparison Operators¶
Equality (=)¶
Check if two values are equal:
JSON:
Template:
{{#if Status = "Active"}}
Your account is active and ready to use.
{{/if}}
{{#if Status = "Pending"}}
Your account is pending approval.
{{/if}}
Tips:
- Use quotes around text values: Status = "Active"
- Numbers don't need quotes: Age = 18
- Comparison is case-sensitive: "active" ≠ "Active"
Inequality (!=)¶
Check if two values are NOT equal:
JSON:
Template:
This message only shows when payment is NOT paid.
Greater Than (>)¶
JSON:
Template:
{{#if Age > 18}}
You are eligible to vote.
{{/if}}
{{#if Score > 90}}
Excellent work! You earned an A grade.
{{/if}}
Less Than (<)¶
JSON:
Template:
{{#if Temperature < 0}}
⚠️ Freezing conditions - take precautions.
{{/if}}
{{#if Stock < 5}}
⚠️ Low stock alert - only {{Stock}} items remaining.
{{/if}}
Greater Than or Equal (>=)¶
JSON:
Template:
{{#if YearsExperience >= 5}}
You qualify for the Senior Developer position.
{{/if}}
{{#if OrderAmount >= MinimumOrder}}
✓ Order qualifies for free shipping!
{{/if}}
Less Than or Equal (<=)¶
JSON:
Template:
{{#if ItemsInCart <= 5}}
Add more items to qualify for bulk discount!
{{/if}}
{{#if DaysUntilExpiry <= 7}}
⚠️ Your subscription expires soon - renew now!
{{/if}}
Logical Operators¶
AND Operator¶
Both conditions must be true:
JSON:
Template:
{{#if Age >= 18 and HasLicense}}
You can rent a car.
{{/if}}
{{#if HasLicense and HasInsurance}}
You're approved for vehicle rental.
{{/if}}
OR Operator¶
At least one condition must be true:
JSON:
Template:
{{#if IsVIP or IsPremium}}
You have access to exclusive features.
{{/if}}
{{#if Role = "Admin" or Role = "Moderator"}}
You have moderation permissions.
{{/if}}
NOT Operator¶
Negates a condition:
JSON:
Template:
{{#if not IsExpired}}
Your subscription is active.
{{/if}}
{{#if not IsBanned}}
Welcome back! Your account is in good standing.
{{/if}}
Combining Multiple Operators¶
JSON:
Template:
{{#if Age >= 18 and (Country = "USA" or HasPassport) and not IsBanned}}
You are eligible to travel internationally.
{{/if}}
Operator precedence:
1. Parentheses ()
2. not
3. Comparison operators (=, !=, >, <, >=, <=)
4. and
5. or
Common Patterns¶
Boolean Flags¶
JSON:
Template:
{{#if ShowHeader}}
=== HEADER SECTION ===
Company Name | Contact Info
{{/if}}
[Main content here]
{{#if ShowFooter}}
=== FOOTER SECTION ===
© 2024 Company Name
{{/if}}
Status Checks¶
JSON:
Template:
Order Status:
{{#if OrderStatus = "Pending"}}
⏳ Your order is being processed.
{{/if}}
{{#if OrderStatus = "Shipped"}}
📦 Your order has been shipped!
{{/if}}
{{#if OrderStatus = "Delivered"}}
✓ Your order was delivered.
{{/if}}
{{#if OrderStatus = "Cancelled"}}
❌ This order was cancelled.
{{/if}}
Tiered Messaging¶
Use {{#elseif}} for cleaner tiered content:
JSON:
Template:
Your Score: {{Score}}
{{#if Score >= 90}}
🏆 Outstanding! You achieved an A grade.
{{#elseif Score >= 80}}
👍 Great job! You achieved a B grade.
{{#elseif Score >= 70}}
✓ Good work! You achieved a C grade.
{{#else}}
📚 Keep studying! You can improve.
{{/if}}
Output:
Tip: Use {{#elseif}} instead of nested {{#if}} blocks for cleaner, more maintainable templates.
Access Control¶
JSON:
Template:
{{#if IsAuthenticated}}
Welcome to the dashboard!
{{#if UserRole = "Admin"}}
[Admin Panel]
- User Management
- System Settings
- Reports
{{/if}}
{{#if UserRole = "Editor"}}
[Editor Panel]
- Edit Content
- Publish Articles
{{/if}}
{{#if UserRole = "Viewer"}}
[Viewer Panel]
- View Content Only
{{/if}}
{{#else}}
Please log in to access this page.
{{/if}}
Working with Numbers¶
Range Checks¶
JSON:
Template:
{{#if Age >= 18 and Age < 65}}
Adult pricing applies.
{{/if}}
{{#if Temperature >= 60 and Temperature <= 80}}
Perfect weather today!
{{/if}}
{{#if Price > 100 and Price <= 200}}
Mid-range product pricing.
{{/if}}
Inventory Checks¶
JSON:
Template:
{{#if StockLevel = 0}}
❌ OUT OF STOCK
{{#else}}
{{#if StockLevel < ReorderPoint}}
⚠️ LOW STOCK: {{StockLevel}} remaining
{{#else}}
✓ In Stock: {{StockLevel}} available
{{/if}}
{{/if}}
Discount Qualification¶
JSON:
Template:
{{#if OrderTotal >= 100 or IsFirstOrder or LoyaltyPoints >= 1000}}
🎉 You qualify for a discount!
{{#if OrderTotal >= 100}}
- Free shipping on orders over $100
{{/if}}
{{#if IsFirstOrder}}
- 15% off first order discount
{{/if}}
{{#if LoyaltyPoints >= 1000}}
- Loyalty member discount available
{{/if}}
{{/if}}
Working with Text¶
Text Comparison¶
Remember: text comparisons are case-sensitive!
JSON:
Template:
{{#if Category = "Electronics"}}
Shipping: 2-3 business days
{{/if}}
{{#if Category = "electronics"}}
This won't match - wrong case!
{{/if}}
{{#if Priority = "high"}}
⚠️ HIGH PRIORITY ORDER
{{/if}}
Multiple Text Options¶
JSON:
Template:
{{#if PaymentMethod = "Credit Card" or PaymentMethod = "Debit Card"}}
Card payment processing fee: $2.50
{{/if}}
{{#if PaymentMethod = "PayPal" or PaymentMethod = "Venmo"}}
Online payment processing fee: 3%
{{/if}}
{{#if PaymentMethod = "Cash" or PaymentMethod = "Check"}}
No processing fees!
{{/if}}
Nested Conditionals¶
You can nest conditionals inside each other:
JSON:
Template:
{{#if IsLoggedIn}}
Welcome!
{{#if UserType = "Premium"}}
{{#if HasActiveSubscription}}
[Premium Content Unlocked]
Access to all features!
{{#else}}
[Subscription Expired]
Please renew your subscription.
{{/if}}
{{#else}}
[Free Account]
Upgrade to Premium for more features.
{{/if}}
{{#else}}
Please log in.
{{/if}}
Best Practice: Limit nesting to 2-3 levels deep to keep templates readable.
Conditionals with Loops¶
You can use conditionals inside loops:
JSON:
{
"Products": [
{ "Name": "Widget", "Price": 10, "InStock": true },
{ "Name": "Gadget", "Price": 25, "InStock": false },
{ "Name": "Doohickey", "Price": 15, "InStock": true }
]
}
Template:
Product List:
{{#foreach Products}}
- {{Name}}: ${{Price}}
{{#if InStock}}
✓ Available
{{#else}}
❌ Out of Stock
{{/if}}
{{/foreach}}
Output:
Loop Variables in Conditionals¶
Use loop-specific variables in conditionals:
JSON:
Template:
{{#foreach Items}}
{{#if @first}}*** First item: {{.}} ***{{/if}}
{{#if not @first and not @last}}- {{.}}{{/if}}
{{#if @last}}*** Last item: {{.}} ***{{/if}}
{{/foreach}}
Output:
Common Use Cases¶
Personalized Greetings¶
JSON:
Template:
Dear {{CustomerName}},
{{#if DaysSinceLastPurchase < 30}}
Great to see you again so soon!
{{#else}}
We've missed you! It's been a while since your last visit.
{{/if}}
{{#if DaysSinceLastPurchase > 60}}
Here's a 15% discount to welcome you back!
{{/if}}
Terms and Conditions¶
JSON:
Template:
TERMS AND CONDITIONS
{{#if IncludeWarranty}}
1. Warranty Coverage
This product includes a 2-year manufacturer warranty...
{{/if}}
{{#if IncludeInsurance}}
2. Insurance Policy
Additional insurance coverage provides...
{{/if}}
{{#if IncludeExtendedSupport}}
3. Extended Support
24/7 customer support is included for...
{{/if}}
Regional Content¶
JSON:
Template:
{{#if Country = "USA"}}
Customer Service: 1-800-555-0123
Business Hours: 9 AM - 5 PM EST
{{/if}}
{{#if Country = "UK"}}
Customer Service: 0800 123 4567
Business Hours: 9 AM - 5 PM GMT
{{/if}}
{{#if Country = "Germany"}}
Kundenservice: 0800 123 4567
Geschäftszeiten: 9:00 - 17:00 Uhr MEZ
{{/if}}
Troubleshooting¶
Conditional Not Working¶
Check these common issues:
- Syntax errors:
- ✅
{{#if Status = "Active"}} - ✅
{{#elseif Status = "Pending"}} - ❌
{{if Status = "Active"}}(missing#) - ❌
{{elseif Status = "Pending"}}(missing#) -
❌
{{#if Status = "Active"(missing closing}}) -
Missing closing tag:
- ✅
{{#if ...}}...{{/if}} -
❌
{{#if ...}}...{{#endif}}(wrong closing tag) -
Case sensitivity:
- ✅
{{#if Status = "Active"}}with JSON:"Status": "Active" -
❌
{{#if Status = "active"}}with JSON:"Status": "Active"} -
Wrong operator:
- ✅
{{#if Age = 18}}(checking equality) -
❌
{{#if Age == 18}}(wrong operator, use single=) -
Quotes around text:
- ✅
{{#if Name = "Alice"}} -
❌
{{#if Name = Alice}}(missing quotes) -
Comparing wrong types:
- ✅
{{#if Age > 18}}with JSON:"Age": 25(number) - ⚠️
{{#if Age > 18}}with JSON:"Age": "25"(string - may not work as expected)
Content Always Shows/Never Shows¶
Debug steps:
-
Print the variable value to see what you're working with:
-
Check JSON structure:
-
Simplify the condition: Start with a simple boolean:
Nested Conditionals Not Working¶
Make sure each {{#if}} has a matching {{/if}}:
❌ Wrong:
✅ Correct:
ElseIf Not Working¶
Common mistakes:
-
Wrong order - else before elseif:
-
Missing
#in elseif: - ✅
{{#elseif Condition}} -
❌
{{elseif Condition}}(missing#) -
Wrong syntax variants:
- ✅
{{#elseif Condition}} - ❌
{{else if Condition}}(wrong - no space) - ❌
{{#else if Condition}}(wrong syntax) - ❌
{{#elif Condition}}(elif is not supported)
Best Practices¶
- Keep conditions simple - Break complex logic into multiple simpler conditions
- Use meaningful variable names -
IsEligibleForDiscountis better thanFlag1 - Test edge cases - What happens when values are null, zero, empty, etc.?
- Add comments in Word - Use Word comments to document complex conditional logic
- Use else clauses - Provide feedback for both true and false cases when appropriate
- Use elseif for multiple conditions - Prefer
{{#elseif}}over deeply nested{{#if}}blocks - Keep else last - The
{{#else}}branch must always be the final branch before{{/if}} - Limit nesting - Deep nesting is hard to read; with
{{#elseif}}, you often don't need nesting at all
Next Steps¶
- Loops Guide - Repeat content for arrays and lists
- Boolean Expressions - Advanced boolean expression techniques
- Placeholders Guide - Using variables in your templates
- Template Syntax Reference - Complete syntax guide
- Examples Gallery - Real-world examples
Related Topics¶
- Format Specifiers - Display boolean values as Yes/No, checkboxes, etc.
- Best Practices - Tips for maintainable templates
- JSON Basics - Understanding your data structure