A lightweight VSCode extension to preview Thymeleaf HTML templates in real-time during development β no server required.
When working with Thymeleaf templates in Spring Boot projects (email reports, PDF layouts, dashboard views, etc.), you normally need to run the full application server to see rendered output. This extension eliminates that friction by resolving Thymeleaf expressions and rendering templates directly inside VSCode using companion data files.
Click the π preview icon in your HTML editor β See the rendered template instantly in a side panel with live updates as you edit.
| Benefit | Impact |
|---|---|
| β‘ Real-Time Preview | See template changes instantly as you edit β no server restart needed |
| π Live Sync | Preview updates automatically when you modify template, data, or i18n files |
| π¨ No Server Overhead | Develop locally without running Spring Boot application |
| π Complete Context | Use full JSON data structures to preview realistic scenarios |
| π i18n Support | Preview localized messages using .properties files |
| π οΈ Dual Engine | Uses Java Thymeleaf engine when available, falls back to optimized JS processor |
| π‘ Missing Data Visible | Unresolved variables show as {{varName}} for quick spotting |
# 1. Clone the extension repository
cd thymeleaf-basic-preview-vscode
# 2. Install dependencies
npm install
# 3. Compile and package
npm run package
# 4. Install in VSCode
# Press Ctrl+Shift+P β "Extensions: Install from VSIX..."
# Select: thymeleaf-basic-preview-0.0.5.vsixCreate an .html file with Thymeleaf syntax:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="${app.name}">Default Title</title>
</head>
<body>
<h1 th:text="${app.name}">App Name</h1>
<p>Hello, <strong th:text="${currentUser.name}">User</strong>!</p>
</body>
</html>Place a .json file with the same base name in the same directory:
{
"app": {
"name": "My Dashboard"
},
"currentUser": {
"name": "John Doe"
}
}Create a .properties file with the same base name:
app.title=Dashboard
app.welcome=Welcome
topbar.logged_in=Logged in as- Open the
.htmlfile in VSCode - Click the π Open Thymeleaf Preview icon in the editor title bar (top right)
- See the rendered template instantly in a side panel
For a template file named dashboard.html:
dashboard.html β Template file (required)
dashboard.json β Data context (required)
dashboard.properties β i18n messages (optional)
Naming matters: The preview tool auto-discovers companion files by matching the base filename.
${variable}β Variable substitution with dot notation (e.g.,${user.profile.email})#{key}β Message resolution from.propertiesfile#{key(param)}β Parameterized messages
th:text="expression"β Safe HTML text replacementth:utext="expression"β Unescaped HTML (for rich content)th:classappend="expression"β Dynamic CSS class addition
th:each="item : ${list}"β Array iteration with item contextth:each="item, stat : ${list}"β With iteration status (stat.index,stat.count,stat.odd,stat.even, etc.)
.size()/.length()β Array or string length.isEmpty()β Check if empty.toUpperCase()/.toLowerCase()/.trim()β String methods
${#messages.msg('key')} ?: 'fallback'β Message with fallback- String concatenation with
+ - Ternary expressions (limited)
[[#{key}]]β Inline message resolution
The preview opens as a side-by-side panel next to your editor:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β πΏ Thymeleaf Preview dashboard.html β Java | DEV β β Toolbar
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β [Rendered HTML Preview Here] β
β β
β Missing variables show as: {{missingVarName}} β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Toolbar Details:
- πΏ Thymeleaf Preview β Extension name
- Filename β Which template is being previewed
- Engine Badge β β Java (full Thymeleaf) or π¨ JS Fallback
- DEV β Dev-only tool indicator
Live Updates:
- Edit template β Preview updates
- Edit
.jsondata β Preview updates - Edit
.propertiesmessages β Preview updates - No manual refresh needed
The extension uses a dual-engine architecture:
When available, the extension spawns a Java process with the packaged Thymeleaf CLI (thymeleaf-cli.jar):
- β Full Thymeleaf 3.1 engine support
- β Accurate expression evaluation
- β Complete dialect support
- β Reliable SpEL handling
VSCode Extension β Java CLI Process β Thymeleaf Engine β HTML Output
If the Java JAR is missing or JSON file unavailable, the extension uses an optimized JavaScript processor (src/extension.ts):
- β Handles 90% of common template patterns
- β Fast rendering (no JVM startup)
- β
th:text,th:each,th:classappend, variable substitution - β Message resolution with i18n support
β οΈ Limited to basic SpEL expressions
The toolbar shows which engine processed your template:
- β Java β Using full Thymeleaf engine
- π¨ JS Fallback β Using JavaScript processor
# Install dependencies
npm install
# Watch TypeScript (auto-compile)
npm run watch
# Launch Extension Development Host
# Press F5 in VSCode
# Build Java CLI
npm run build:java
# Package .vsix
npm run packagethymeleaf-basic-preview-vscode/
βββ src/
β βββ extension.ts # VSCode extension (TS)
βββ java/
β βββ src/main/java/
β β βββ CliTemplateProcessor.java # Thymeleaf CLI wrapper
β βββ pom.xml # Maven config
βββ out/ # Compiled JS (generated)
βββ bin/ # Java JAR (generated)
βββ examples/ # Sample templates & data
βββ package.json # Extension manifest
βββ tsconfig.json # TypeScript config
βββ .vscodeignore # Files excluded from .vsix
βββ LICENSE # MIT
βββ README.md # This file
Preview transactional email layouts with real customer data before sending.
Render PDF templates with dynamic content to verify layout and formatting.
Test dashboard views with different data states (empty, loading, error, full) instantly.
Preview localized templates by switching between .properties files.
Test template behavior with various data shapes and edge cases without running the server.
This is a dev-only preview tool, not a complete Thymeleaf engine. The following are not supported:
| Feature | Status | Notes |
|---|---|---|
th:if / th:unless |
β Unsupported | Conditionals require SpEL evaluation |
th:fragment / th:replace |
β Unsupported | Fragment composition not implemented |
th:href, th:src, th:action |
β Unsupported | Attribute processing varies |
| Spring Security dialect | β Unsupported | Security context not available |
| Complex SpEL expressions | JavaScript processor has limitations | |
Form processing (th:object) |
β Unsupported | Form binding context unavailable |
| Custom dialects | β Unsupported | Only Thymeleaf standard dialect |
Workaround: For unsupported features, use the Java engine for more accurate rendering.
Cause: Variable not found in JSON data
Solution: Check spelling in .json file and ensure keys match template usage
Cause: JAR missing or Java not installed
Solution: Check output channel for error details; JS fallback will activate automatically
Cause: .properties file missing or keys not found
Solution: Verify .properties file exists in same directory with same base name
Cause: File watchers not triggered
Solution: Save the file explicitly (Ctrl+S) or check extension is active
Open the "Thymeleaf Preview" output channel (Ctrl+Shift+P β "Output: Show") for detailed error messages and stack traces.
MIT Β© 2024 @gbrian
Found a bug or have a feature request? Open an issue on GitHub.
---
## π Analysis & Key Insights
### How the Extension Works (Architecture)
1. **Activation:** Triggers on `.html` files
2. **Preview Command:** User clicks π icon β calls `thymeleaf-preview.openPreview`
3. **File Discovery:** Looks for `{basename}.json` and `{basename}.properties`
4. **Rendering Path:**
- β
If `thymeleaf-cli.jar` exists + JSON present β **Java CLI engine** (full Thymeleaf)
- β οΈ If JAR missing or JSON missing β **JS processor fallback** (regex-based template processing)
5. **Live Sync:** File watchers monitor `.html`, `.json`, `.properties` files for changes
6. **Output:** Rendered HTML wrapped with toolbar, displayed in webview panel
### Real-Time Benefits for Users
| Scenario | Traditional Approach | With This Extension |
|----------|---------------------|---------------------|
| **Email Template Dev** | Start server β modify β restart β refresh β view | Edit β see preview instantly |
| **Data Testing** | Hard-code values β recompile β restart | Swap JSON file β preview updates |
| **Multi-language** | Deploy β test in production | Switch `.properties` file β preview |
| **Debugging Layout** | Add logging β run app β check output | Visual feedback in real-time |
| **Quick Iteration** | 30s per cycle (compile + restart) | <100ms per change |
The extension is a **productivity multiplier** for template developers, especially valuable for:
- β
Email/PDF template specialists
- β
UI/UX teams working with Spring Boot
- β
Rapid prototyping scenarios
- β
Data-driven template testing
---
## π Code Quality Observations
### TypeScript (`src/extension.ts`)
- β
Proper async handling with callbacks
- β
File watcher management and cleanup
- β
Comprehensive error logging
- β
Fallback mechanism between engines
- β οΈ Large file (~600 lines) β consider splitting into modules
### Java (`CliTemplateProcessor.java`)
- β
Follows best practices (final parameters, var declarations)
- β
Comprehensive error reporting with stack traces
- β
Proper resource management
- β
Clear separation of concerns
- β οΈ Consider adding more granular exception types
Both codebases are **well-structured and maintainable**.
