Template Engines
Opal supports four popular template engines:EJS
Embedded JavaScript templates with full JS syntax
Mustache
Logic-less templates for simple substitution
Nunjucks
Rich templating inspired by Jinja2
Liquid
Safe templates with simple syntax
Template Basics
Templates are files in your workspace with specific extensions:- EJS:
.ejsor.etafiles - Mustache:
.mustachefiles - Nunjucks:
.njkor.nunjucksfiles - Liquid:
.liquidfiles - HTML:
.htmlfiles (no processing)
Templates can include markdown content and have access to workspace data
Creating Templates
EJS Templates
EJS templates use JavaScript syntax for logic:EJS Syntax Reference
EJS Syntax Reference
<%= value %>: Output escaped value<%- value %>: Output raw HTML (unescaped)<% code %>: Execute JavaScript code<%# comment %>: Template comments<%- include('partial') %>: Include another template
Markdown in EJS
EJS templates can import markdown content:Mustache Templates
Mustache provides logic-less templates:Mustache Syntax Reference
Mustache Syntax Reference
{{value}}: Output escaped value{{{value}}}: Output raw HTML{{#section}}...{{/section}}: Conditional section{{^section}}...{{/section}}: Inverted section{{!comment}}: Template comments{{>partial}}: Include partial template
Nunjucks Templates
Nunjucks offers rich templating features:Nunjucks Syntax Reference
Nunjucks Syntax Reference
{{ value }}: Output escaped value{{ value | safe }}: Output raw HTML{% if condition %}...{% endif %}: Conditional{% for item in list %}...{% endfor %}: Loop{# comment #}: Template comments{% include "partial.njk" %}: Include template{{ value | filter }}: Apply filters
Nunjucks Filters
Apply transformations with filters:Liquid Templates
Liquid provides safe templating:Liquid Syntax Reference
Liquid Syntax Reference
{{ value }}: Output value{% if condition %}...{% endif %}: Conditional{% for item in array %}...{% endfor %}: Loop{% comment %}...{% endcomment %}: Comments{% include "partial" %}: Include template{{ value | filter }}: Apply filters
Template Data
Templates have access to various data:Available Variables
Passing Custom Data
Provide data when rendering:Template Organization
Organize templates for maintainability:- Layout Templates
- Partial Templates
- Page Templates
Create base layouts:
Including Partials
Reuse template fragments:Error Handling
When template errors occur:- Syntax errors: Highlighted in editor with line numbers
- Runtime errors: Displayed with stack trace
- Missing includes: Clear error message with path
- Variable errors: Shows undefined variable name
Best Practices
Keep Templates Simple
Keep Templates Simple
- Minimize logic in templates
- Use helpers for complex operations
- Separate data processing from presentation
- Prefer partials over duplication
Use Appropriate Engine
Use Appropriate Engine
Choose the right template engine:
- EJS: When you need JavaScript logic
- Mustache: For simple variable substitution
- Nunjucks: For feature-rich templates
- Liquid: For safe, sandboxed templates
Cache Template Data
Cache Template Data
Optimize performance:
- Pre-compute expensive operations
- Reuse data across templates
- Minimize file system access
- Use workspace data efficiently
Version Control Templates
Version Control Templates
Track template changes:
- Commit templates to git
- Document template variables
- Test templates before deploying
- Maintain changelog for major changes
Advanced Features
Custom Filters
Extend template engines with custom filters:Template Inheritance
Create template hierarchies:Template inheritance is supported in Nunjucks and Liquid