How to Fix GitBook Variable Syntax Errors
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
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro