Template Engine Syntax
Master Staticraft zero-dependency template engine syntax, layout inheritance, slot injection, component partials, loops, conditionals, and automatic XSS escaping.
1. Layout Inheritance & {{ slot }} Injection
Define a shared master layout in `src/app/layouts/base.html` containing common HTML head elements, headers, and footers. Child pages reference the layout using `{{ layout "layouts/base.html" }}`. Page contents are automatically injected into `{{ slot }}`.
<!-- src/app/layouts/base.html -->
<!DOCTYPE html>
<html>
<head><title>{{ title }}</title></head>
<body>
<header>Site Header</header>
<main>{{ slot }}</main>
</body>
</html>
<!-- src/app/page.html -->
{{ layout "layouts/base.html" }}
<h1>Welcome to {{ title }}</h1>
2. Reusable Components & Partials
Break modular UI components into clean template files and include them using `{{ component "components/navbar.html" }}`.
{{ component "components/navbar.html" }}
<section>Content Here</section>
{{ component "components/footer.html" }}
3. Conditionals ({{#if}})
Evaluate boolean or object presence conditionally with `{{#if variable}}...{{/if}}`.
{{#if isFeatured}}
<span class="badge">Featured Post</span>
{{/if}}
4. Array Loops ({{#each}})
Iterate over lists using `{{#each items}}...{{/each}}`. Object properties inside the array are exposed directly in scope.
<ul>
{{#each features}}
<li>
<strong>{{ title }}</strong> - {{ description }}
</li>
{{/each}}
</ul>
5. Variable Escaping & Asset Fingerprinting
By default, `{{ variable }}` applies contextual XSS HTML escaping. Use `{{{ rawHtml }}}` for unescaped HTML interpolation. All non-HTML static assets (CSS, JS, images) are automatically hashed (e.g., `styles.07eef7b1.css`) and rewritten across templates. To keep specific assets unhashed (such as favicons or Open Graph social images), specify `ignoreHash: ["favicon.png", "og-image.png"]` in `staticraft.config.js`.
<link rel="stylesheet" href="/styles.css">
<!-- Transformed automatically into: -->
<link rel="stylesheet" href="/styles.07eef7b1.css">