Overview
simple_template is a Mustache-style template engine for Eiffel. It provides logic-less templates with automatic HTML escaping to prevent XSS attacks, sections for conditionals and loops, and partial template support.
Part of the simple_* ecosystem of focused, single-purpose Eiffel libraries.
Quick Start
Installation
Add to your ECF file:
<library name="simple_template" location="$SIMPLE_TEMPLATE/simple_template.ecf"/>
Set environment variable:
set SIMPLE_TEMPLATE=D:\path\to\simple_template
Basic Variable Substitution
local
tpl: SIMPLE_TEMPLATE
do
create tpl.make_from_string ("Hello, {{name}}!")
tpl.set_variable ("name", "World")
print (tpl.render) -- "Hello, World!"
end
HTML Escaping (Default)
-- Automatic XSS prevention
tpl.set_variable ("content", "<script>alert('xss')</script>")
print (tpl.render) -- "<script>alert('xss')</script>"
Raw/Unescaped Output
-- Triple braces for raw HTML
create tpl.make_from_string ("{{{html}}}")
tpl.set_variable ("html", "<b>Bold</b>")
print (tpl.render) -- "<b>Bold</b>"
Conditional Sections
-- Show section when truthy
create tpl.make_from_string ("{{#logged_in}}Welcome back!{{/logged_in}}")
tpl.set_section ("logged_in", True)
print (tpl.render) -- "Welcome back!"
-- Inverted section (show when falsy)
create tpl.make_from_string ("{{^has_items}}No items{{/has_items}}")
tpl.set_section ("has_items", False)
print (tpl.render) -- "No items"
List Iteration
local
items: ARRAYED_LIST [HASH_TABLE [STRING, STRING]]
item: HASH_TABLE [STRING, STRING]
do
create tpl.make_from_string ("{{#users}}{{name}} {{/users}}")
create items.make (2)
create item.make (1)
item.put ("Alice", "name")
items.extend (item)
create item.make (1)
item.put ("Bob", "name")
items.extend (item)
tpl.set_list ("users", items)
print (tpl.render) -- "Alice Bob "
end
Comments
-- Comments are not rendered
create tpl.make_from_string ("Hello{{! This is ignored }}World")
print (tpl.render) -- "HelloWorld"
Partials
local
tpl, header: SIMPLE_TEMPLATE
do
create header.make_from_string ("<h1>{{title}}</h1>")
create tpl.make_from_string ("{{>header}}<main>Content</main>")
tpl.register_partial ("header", header)
tpl.set_variable ("title", "My Page")
print (tpl.render)
end
Template Syntax
| Syntax | Description |
|---|---|
{{variable}} |
Output variable (HTML escaped) |
{{{variable}}} |
Output variable (raw, no escaping) |
{{#section}}...{{/section}} |
Conditional section (show if truthy) or list iteration |
{{^section}}...{{/section}} |
Inverted section (show if falsy) |
{{! comment }} |
Comment (not rendered) |
{{>partial}} |
Include partial template |
Features
Mustache Syntax
Standard Mustache template syntax compatible with other implementations.
Auto HTML Escaping
XSS prevention by default. Use triple braces for raw output.
Sections
Conditional rendering with truthy/falsy evaluation.
Inverted Sections
Show content only when value is false or missing.
List Iteration
Repeat sections for each item in a list.
Partials
Include reusable sub-templates.
Comments
Template documentation that doesn't appear in output.
Design by Contract
Full preconditions, postconditions, and class invariants.
API Summary
Initialization
| Feature | Description |
|---|---|
make |
Create empty template |
make_from_string (template) |
Create from string |
make_from_file (path) |
Load from file |
Configuration
| Feature | Description |
|---|---|
set_escape_html (enabled) |
Enable/disable HTML escaping |
set_missing_variable_policy (policy) |
Set behavior for missing variables |
register_partial (name, template) |
Register a partial template |
Context Building
| Feature | Description |
|---|---|
set_variable (name, value) |
Set a variable |
set_variables (table) |
Set multiple variables |
set_section (name, visible) |
Set section visibility |
set_list (name, items) |
Set list for iteration |
clear_variables |
Clear all context |
Rendering
| Feature | Description |
|---|---|
render: STRING |
Render template to string |
render_to_file (path) |
Render and write to file |
Query
| Feature | Description |
|---|---|
has_variable (name): BOOLEAN |
Is variable defined? |
required_variables: LIST |
Extract variable names from template |
is_valid: BOOLEAN |
Is template syntactically valid? |
last_error: STRING |
Last error message |
template_source: STRING |
The template string |
escape_html_enabled: BOOLEAN |
Is escaping enabled? |
Missing Variable Policies
| Constant | Description |
|---|---|
Policy_empty_string |
Missing vars become "" (default) |
Policy_keep_placeholder |
Keep {{name}} in output |
Policy_raise_exception |
Set error on missing var |
Testing
The library includes a comprehensive test suite with 32 tests covering:
- Initialization (empty, string, file)
- Configuration (escaping, missing variable policy)
- Variable handling (single, multiple, clear)
- Basic rendering (plain text, variables, spaces)
- HTML escaping (entities, raw output, disabled)
- Sections (truthy, falsy, missing, inverted)
- List iteration (items, empty list)
- Comments (single line, multiline)
- Partials
- Nested sections
-- Run tests
ec.exe -batch -config simple_template.ecf -target simple_template_tests -c_compile
./EIFGENs/simple_template_tests/W_code/simple_template.exe