Skip to content

How to Fix GitBook Variable Syntax Errors

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix GitBook Variable Syntax Errors. We cover key concepts, practical examples, and best practices.

GitBook variables like {{ variableName }} show as raw text instead of being substituted. Variables work only in specific contexts and with correct syntax.

The Wrong Way

// Using JavaScript template literal syntax in content
page.content = `Hello ${userName}`;

GitBook uses Nunjucks-style {{ }} syntax, not JavaScript ${ }. The wrong syntax is rendered verbatim.

The Right Way

Step 1: Define variables in space variables

# GitBook Space → Settings → "Variables"
# Add variables:
# - Name: company_name
# - Value: DodaTech
# - Name: product_name
# - Value: Doda Browser

Step 2: Use correct Nunjucks syntax

# In page content:
# {{ company_name }} → renders as "DodaTech"
# {{ product_name }} → renders as "Doda Browser"

# With filters:
# {{ company_name | upper }} → "DODATECH"
# {{ company_name | lower }} → "dodatech"

Step 3: Handle variables in code blocks

# Variables inside code blocks are NOT processed
# ```<a href="/programming-languages/javascript/">javascript</a>
# const name = "{{ company_name }}"; // Still renders as raw text
# ```

# Workaround: use GitBook's "variable" shortcode:
# {% raw %}{{ company_name }}{% endraw %} → "{{ company_name }}" in output

Step 4: Use conditional variables

# {% if product_name %}
#   Welcome to {{ product_name }}
# {% endif %}

# This only renders the sentence if product_name is defined and non-empty.
"Welcome to Doda Browser" renders correctly — variable substitution working, filters applied.

Prevention

  • Define all variables in Space Settings before using them in content.
  • Test variable rendering in a preview page before publishing.
  • The variable substitution engine is similar to Doda Browser's template bookmark system, where {{ title }} and {{ url }} are replaced with actual values.

Common Mistakes with variable syntax

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks

These mistakes appear frequently in real-world GITBOOK code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why are my GitBook variables showing as raw {{ text }}?

Three common causes: (1) the variable is not defined in Space Settings, (2) the variable is inside a code block, or (3) you used single braces {variable} instead of double braces {{ variable }}. Double braces are required.

Can I use GitBook variables in page titles?

No. GitBook variables only work in page body content. Page titles and SUMMARY.md entries do not support variable substitution. Use static text for titles.

Do GitBook variables support nested objects?

No. GitBook variables are flat key-value pairs. You cannot reference nested properties like {{ user.name }}. Define variables as user_name instead.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro