Doc Variables
← Back to Resources

Variable Syntax Cheat Sheet for Doc Variables

Variable Syntax Cheat Sheet for Doc Variables

Variable Syntax Cheat Sheet for Doc Variables

Master Template Variables, Conditionals, and Loops in Google Docs

You've heard about template variables. Maybe you've even used basic ones like `{{Name}}` or `{{Date}}`. But when you need conditional content, repeating sections, or complex data transformations, basic variables hit their limit fast.

This is your complete reference for Doc Variables syntax—from simple variable replacement to advanced conditional logic, loops, and data formatting. Bookmark this page. You'll need it.

Why Syntax Matters

Template automation breaks down when syntax is wrong. A misplaced bracket, forgotten hash, or typo in a variable name means:

  • Silent failures — Variables don't replace, content stays broken
  • Debugging hell — "Why isn't this working?" repeated 50 times
  • Wasted time — Re-generating documents because syntax was off
  • Client embarrassment — Sending proposals with `{{CLIENT_NAME}}` still visible

Good syntax = reliable automation. This guide shows you exactly what works.

The Basics: Simple Variable Replacement

Standard Variable Syntax

The most common format:

{{Variable Name}}

Examples:


Dear {{First Name}},

Thank you for your order #{{Order ID}} on {{Order Date}}.
Your total of ${{Amount}} will be delivered to {{Address}}.

Rules:

  • Double curly braces: `{{` and `}}`
  • Variable names match column headers in your data source (Google Sheet, form, database)
  • Case-sensitive: `{{Name}}` ≠ `{{name}}` ≠ `{{NAME}}`
  • Spaces allowed: `{{First Name}}` works fine
  • Special characters avoided: Stick to letters, numbers, spaces, underscores

What Gets Replaced

When you generate a document:

  1. Doc Variables finds all `{{Variable Name}}` instances
  2. Looks up "Variable Name" in your data source
  3. Replaces the entire `{{Variable Name}}` with the value

Data source example (Google Sheet):

First NameLast NameOrder IDAmount
SarahJohnson10482247.50

Template:

Dear {{First Name}} {{Last Name}},

Your order #{{Order ID}} totaling ${{Amount}} has been processed.

Result:

Dear Sarah Johnson,

Your order #10482 totaling $247.50 has been processed.

Variable Naming Best Practices

Good variable names:

  • `{{Client Name}}`
  • `{{Project Start Date}}`
  • `{{Total Amount Due}}`
  • `{{Service Tier}}`

Bad variable names:

  • `{{V1}}` — Not descriptive
  • `{{Client_Name_Final_v3}}` — Too verbose
  • `{{Name (Legal)}}` — Parentheses can cause issues
  • `{{$Amount}}` — Dollar sign may conflict

Rule of thumb: If you'll forget what the variable means in 3 months, the name is bad.

Conditionals: Show Content Based on Data

Conditionals let you include or exclude content based on variable values.

Basic If Statement

{{#if Variable Name}}
Content to show if variable is true/exists
{{/if}}

Example:

{{#if Premium Member}}
PREMIUM BENEFITS

As a premium member, you receive:
- Priority support
- Exclusive features
- Monthly credits
{{/if}}

Behavior:

  • If "Premium Member" = true, YES, 1, or any non-empty value → content shows
  • If "Premium Member" = false, NO, 0, or empty → content hidden

If-Else Statement

{{#if Variable Name}}
Content if true
{{else}}
Content if false
{{/if}}

Example:

{{#if Payment Status == "Paid"}}
Thank you! Your payment has been received.
{{else}}
Payment is still pending. Please complete checkout.
{{/if}}

Comparison Operators

You can check specific values:


{{#if Service Tier == "Premium"}}Premium content{{/if}}

{{#if Age > 18}}Adult content{{/if}}

{{#if Stock <= 10}}Low stock warning{{/if}}

{{#if Region != "International"}}Domestic shipping{{/if}}

Supported operators:

  • `==` — Equals
  • `!=` — Not equals
  • `>` — Greater than
  • `<` — Less than
  • `>=` — Greater than or equal
  • `<=` — Less than or equal

Multiple Conditions (AND/OR)


{{#if (Premium Member) AND (Active)}}
Content for active premium members only
{{/if}}

{{#if (Region == "US") OR (Region == "Canada")}}
North American shipping rates apply
{{/if}}

Nested Conditionals

Conditionals inside conditionals:

{{#if Service Tier == "Premium"}}

PREMIUM FEATURES

{{#if Add-On Selected == "Extra Storage"}}
You've added 1TB extra storage.
{{/if}}

{{#if Add-On Selected == "Priority Support"}}
You've added priority support access.
{{/if}}

{{/if}}

Use case: Show different content based on multiple variables.

Common Conditional Patterns

Show section only if data exists:

{{#if Notes}}
ADDITIONAL NOTES

{{Notes}}
{{/if}}

Different pricing for different tiers:

{{#if Tier == "Basic"}}Price: $29/month{{/if}}
{{#if Tier == "Pro"}}Price: $79/month{{/if}}
{{#if Tier == "Enterprise"}}Price: Custom{{/if}}

Region-specific content:

{{#if Country == "US"}}
Shipping: USPS Priority Mail, 3-5 business days
{{/if}}

{{#if Country == "Canada"}}
Shipping: Canada Post, 5-10 business days
{{/if}}

{{#if Country != "US" AND Country != "Canada"}}
International shipping: 10-20 business days
{{/if}}

Loops: Repeat Content for Lists

Loops let you iterate over lists and generate repeating sections.

Basic Loop (Each)

{{#each List Variable}}
Content repeated for each item
{{/each}}

Example: List of deliverables

Data source (comma-separated in spreadsheet):

Deliverables
New homepage, Blog design, Contact form, SEO optimization

Template:

PROJECT DELIVERABLES

{{#each Deliverables}}
- {{this}}
{{/each}}

Result:

PROJECT DELIVERABLES

- New homepage
- Blog design
- Contact form
- SEO optimization

Accessing List Items

Inside a loop, use `{{this}}` to reference the current item:

{{#each Products}}
Product: {{this}}
{{/each}}

Loops with Structured Data

If your list contains objects (multiple fields per item):

{{#each Line Items}}
- {{Name}}: ${{Price}} x {{Quantity}} = ${{Total}}
{{/each}}

Data structure (JSON example):

[
  {"Name": "Widget A", "Price": 25, "Quantity": 3, "Total": 75},
  {"Name": "Widget B", "Price": 40, "Quantity": 1, "Total": 40}
]

Result:

- Widget A: $25 x 3 = $75
- Widget B: $40 x 1 = $40

Conditional Inside Loop

Show different content per item based on conditions:

{{#each Products}}
- {{Name}}: ${{Price}}
  {{#if In Stock == "Yes"}}Available now{{else}}Out of stock{{/if}}
{{/each}}

Numbering Loop Items

Use `{{@index}}` for zero-based index or `{{@number}}` for 1-based:

{{#each Tasks}}
{{@number}}. {{Task Name}} - Due: {{Due Date}}
{{/each}}

Result:

1. Website design - Due: March 15
2. Content writing - Due: March 20
3. SEO setup - Due: March 25

Formatting: Control How Data Displays

Date Formatting

Raw date variables often display as numbers or awkward formats. Use date formatters:

{{Date | date: "MMMM DD, YYYY"}}

Examples:

  • `{{Order Date | date: "MM/DD/YYYY"}}` → 03/02/2026
  • `{{Delivery Date | date: "MMMM DD, YYYY"}}` → March 15, 2026
  • `{{Event Date | date: "dddd, MMMM Do"}}` → Monday, March 2nd

Format tokens:

  • `YYYY` — 4-digit year (2026)
  • `MM` — 2-digit month (03)
  • `DD` — 2-digit day (02)
  • `MMMM` — Full month name (March)
  • `MMM` — Short month (Mar)
  • `dddd` — Full day name (Monday)
  • `ddd` — Short day (Mon)

Number Formatting

Add thousand separators, decimals, currency symbols:

{{Amount | currency}}  →  $1,247.50

{{Quantity | number}}  →  1,234  (with commas)

{{Percentage | percent}}  →  85%

Text Transformations


{{Name | uppercase}}  →  SARAH JOHNSON

{{Name | lowercase}}  →  sarah johnson

{{Name | capitalize}}  →  Sarah Johnson

{{Email | trim}}  →  (removes leading/trailing spaces)

Default Values

Show fallback text if variable is empty:

{{Notes | default: "No additional notes provided"}}

Result if Notes is empty: "No additional notes provided"

Result if Notes has value: The actual notes content

Advanced: Combining Multiple Features

Conditional + Loop

Show a list only if data exists:

{{#if Deliverables}}
PROJECT DELIVERABLES

{{#each Deliverables}}
- {{this}}
{{/each}}
{{/if}}

Use case: Prevent empty sections from appearing in documents.

Loop + Conditional + Formatting

Real-world invoice line items:

{{#each Line Items}}
{{@number}}. {{Product Name}} - ${{Unit Price | currency}}
   Quantity: {{Quantity}}
   Subtotal: ${{Subtotal | currency}}
   
   {{#if Discount > 0}}
   Discount: -${{Discount | currency}}
   {{/if}}
   
   Total: ${{Total | currency}}
{{/each}}

Nested Loops

Loop inside another loop (use sparingly—readability suffers):

{{#each Departments}}
DEPARTMENT: {{Department Name}}

{{#each Employees}}
  - {{Name}}, {{Role}}
{{/each}}

{{/each}}

Common Mistakes and How to Fix Them

Mistake 1: Variable Name Doesn't Match

Problem: `{{Client Name}}` in template, but spreadsheet column is `Client_Name`

Result: Variable doesn't replace, `{{Client Name}}` shows in final doc

Fix: Match exactly—spelling, spacing, capitalization

Mistake 2: Missing Closing Tags

Problem:

{{#if Premium}}
Premium content
// Missing {{/if}}

Result: Everything after the `{{#if}}` gets hidden or shows incorrectly

Fix: Always close conditionals and loops with `{{/if}}` or `{{/each}}`

Mistake 3: Incorrect Operator Syntax

Problem: `{{#if Service = "Premium"}}` (single `=`)

Fix: Use `==` for comparison: `{{#if Service == "Premium"}}`

Mistake 4: Spaces in Comparisons

Problem: Data has trailing space: "Premium " vs "Premium"

Result: `{{#if Tier == "Premium"}}` doesn't match "Premium "

Fix: Clean data in spreadsheet (use `TRIM()` function) or use `{{Variable | trim}}` in template

Mistake 5: Confusing `{{this}}` Context

Problem: Using `{{Item}}` inside loop instead of `{{this}}`

Fix: Inside `{{#each}}`, the current item is always `{{this}}`

Testing Your Syntax

Before generating 100 documents:

  1. Test with 1-2 rows of data
  2. Check all variables replaced (search document for `{{` to find leftover variables)
  3. Verify conditionals show/hide correctly
  4. Check loops generate expected number of items
  5. Inspect formatting (dates, numbers, currency)

Catch syntax errors in test docs, not in 100 client proposals.

Real-World Template Examples

Example 1: Conditional Proposal Sections

PROPOSAL FOR {{Client Name}}

PROJECT SCOPE
{{Project Description}}

TIMELINE
Estimated completion: {{Timeline}}

{{#if Rush Job == "Yes"}}
RUSH DELIVERY
This project includes expedited delivery. Additional rush fee of ${{Rush Fee}} applies.
{{/if}}

PRICING
{{#if Service Tier == "Basic"}}
Basic Package: ${{Basic Price}}
{{/if}}

{{#if Service Tier == "Premium"}}
Premium Package: ${{Premium Price}}
Includes: Priority support, dedicated account manager, monthly strategy calls
{{/if}}

{{#if Service Tier == "Enterprise"}}
Enterprise Package: Custom pricing
Contact {{Sales Rep Name}} at {{Sales Rep Email}} for details.
{{/if}}

{{#if Discount > 0}}
SPECIAL OFFER
Discount applied: -${{Discount}}
Final price: ${{Final Price}}
{{/if}}

Example 2: Invoice with Line Items

INVOICE #{{Invoice Number}}

Bill To:
{{Client Name}}
{{Client Address}}
{{Client City}}, {{Client State}} {{Client Zip}}

Invoice Date: {{Invoice Date | date: "MMMM DD, YYYY"}}
Due Date: {{Due Date | date: "MMMM DD, YYYY"}}

LINE ITEMS

{{#each Line Items}}
{{@number}}. {{Description}}
   Unit Price: ${{Unit Price | currency}}
   Quantity: {{Quantity}}
   Total: ${{Line Total | currency}}
{{/each}}

TOTALS

Subtotal: ${{Subtotal | currency}}
{{#if Tax > 0}}
Tax ({{Tax Rate}}%): ${{Tax | currency}}
{{/if}}
{{#if Discount > 0}}
Discount: -${{Discount | currency}}
{{/if}}

TOTAL DUE: ${{Total | currency}}

{{#if Payment Terms}}
Payment Terms: {{Payment Terms}}
{{/if}}

Thank you for your business!

Example 3: Personalized Event Invitations

Dear {{First Name}},

You're invited to {{Event Name}}!

Date: {{Event Date | date: "dddd, MMMM Do, YYYY"}}
Time: {{Event Time}}
Location: {{Venue Name}}, {{Venue Address}}

{{#if VIP == "Yes"}}
VIP ACCESS

As a VIP guest, you'll enjoy:
- Early entry at {{VIP Entry Time}}
- Access to exclusive VIP lounge
- Complimentary drinks and appetizers
- Meet-and-greet with {{Special Guest}}
{{/if}}

{{#if Dietary Restrictions}}
We've noted your dietary preference: {{Dietary Restrictions}}
{{/if}}

RSVP by {{RSVP Deadline | date: "MMMM DD"}} at {{RSVP URL}}

Looking forward to seeing you!

Best regards,
{{Event Organizer}}
{{Contact Email}}

Quick Reference: Syntax Cheat Sheet

FeatureSyntaxExample
Basic Variable`{{Variable}}``{{Name}}`
If Statement`{{#if Variable}}...{{/if}}``{{#if Active}}Show this{{/if}}`
If-Else`{{#if Var}}...{{else}}...{{/if}}``{{#if Paid}}Paid{{else}}Pending{{/if}}`
Comparison`{{#if Var == "Value"}}``{{#if Tier == "Pro"}}`
Loop`{{#each List}}...{{/each}}``{{#each Items}}{{this}}{{/each}}`
Current Item`{{this}}`Inside `{{#each}}`
Index`{{@index}}` or `{{@number}}``{{@number}}. {{Name}}`
Date Format`{{Var | date: "format"}}``{{Date | date: "MM/DD/YYYY"}}`
Currency`{{Var | currency}}``{{Price | currency}}`
Uppercase`{{Var | uppercase}}``{{Name | uppercase}}`
Default Value`{{Var | default: "text"}}``{{Notes | default: "None"}}`

When to Use What

Use simple variables when:

  • Content is always the same structure
  • Every field is always filled
  • No conditional logic needed

Use conditionals when:

  • Some clients get different content (pricing tiers, regions, membership levels)
  • Optional sections (only show if data exists)
  • Different language/copy based on variables

Use loops when:

  • Variable number of items (invoice line items, deliverables, attendees)
  • Repeating sections with different data
  • Generating lists from databases

Performance Tips

  • Keep conditionals simple — Deeply nested conditionals slow generation and make debugging hard
  • Avoid huge loops — 100+ items in a loop? Consider splitting into multiple documents
  • Test incrementally — Add one feature at a time, test, then add more
  • Use comments — Add notes in your template so you remember what complex syntax does

Getting Help

If syntax isn't working:

  1. Check closing tags — Every `{{#if}}` needs `{{/if}}`, every `{{#each}}` needs `{{/each}}`
  2. Verify variable names — Copy-paste from your data source to template to ensure exact match
  3. Test with simple data — Use "Test" or "Sample" as values to isolate syntax vs data issues
  4. Search for leftover variables — Find `{{` in your generated document to spot unfilled variables
  5. Check Doc Variables documentation — docvars.com/docs has syntax examples and troubleshooting

Final Thoughts

Template syntax looks intimidating at first. Curly braces, hash marks, conditionals, loops—it feels like learning code. But once you internalize the patterns, it becomes second nature.

Start simple: Basic variables. Get comfortable with those. Then add one conditional. Then a loop. Build complexity gradually.

This cheat sheet is your reference. Bookmark it. Copy examples. Adapt them to your use cases. And remember: Every syntax error is just a learning opportunity that makes you faster next time.

Your templates will thank you.


Doc Variables makes advanced template automation simple. Use variables, conditionals, and loops to create personalized Google Docs at scale—no coding required. Try it free with 20 document generations at docvars.com.

Ready to try Doc Variables?

Join 190,000+ users creating amazing Google Doc templates.

Install Now - It's Free