- Core Functions
- Template Registration
- Error Handling
- Component System
- Helper System
- Template Inheritance
- Syntax Reference
Compiles a template string into an optimized rendering function.
Parameters:
template(string): The template string to compileoptions(CompilerOptions, optional): Compilation optionsescape(boolean): Whether to HTML-escape output (default: true)
templateName(string, optional): Name for error reporting
Returns: (data: unknown) => string - Compiled template function
Example:
import { compile } from "./mod.ts";
const template = `<h1>{{title}}</h1>
<p>Welcome {{user.name}}!</p>`;
const render = compile(template, { escape: true }, "welcomeTemplate");
const html = render({
title: "My Site",
user: { name: "Alice" },
});Legacy function for backwards compatibility. Compiles and renders a template in one call.
Parameters:
key(string): Template identifier (unused, kept for compatibility)data(unknown): Data to rendertemplate(string): Template string
Returns: string - Rendered HTML
Registers a layout template for use with {{> layoutName}} syntax.
Example:
import { registerLayout } from "./mod.ts";
registerLayout(
"mainLayout",
`
<html>
<head><title>{{title}}</title></head>
<body>
{{> content}}
</body>
</html>
`,
);Registers a reusable component template.
Example:
import { registerComponent } from "./mod.ts";
registerComponent(
"userCard",
`
<div class="user-card">
<h3>{{name}}</h3>
<p>{{email}}</p>
<span class="{{@parent.theme}}-badge">{{role}}</span>
</div>
`,
);Registers a base template for inheritance.
Example:
import { registerBaseTemplate } from "./mod.ts";
registerBaseTemplate(
"basePage",
`
<!DOCTYPE html>
<html>
<head>
<title>{{#block "title"}}Default Title{{/block}}</title>
</head>
<body>
<header>{{#block "header"}}Default Header{{/block}}</header>
<main>{{#block "content"}}Default Content{{/block}}</main>
<footer>{{#block "footer"}}Default Footer{{/block}}</footer>
</body>
</html>
`,
);Clears all compiled template caches. Useful for development or when templates change dynamically.
Base class for all template-related errors.
Properties:
line(number, optional): Line number where error occurredcolumn(number, optional): Column number where error occurredtemplateName(string, optional): Name of the templatecontext(string, optional): Contextual code around the error
Thrown when template syntax is invalid.
Example:
try {
const render = compile("{{#if unclosed block");
} catch (error) {
if (error instanceof TemplateSyntaxError) {
console.log(`Syntax error at line ${error.line}: ${error.message}`);
console.log(error.context);
}
}Thrown when template execution fails.
Components receive props as their data context:
registerComponent(
"productCard",
`
<div class="product">
<h3>{{name}}</h3>
<p class="price">\${{price}}</p>
<p class="description">{{description}}</p>
</div>
`,
);Access parent template data using @parent:
Components can use other components:
registerComponent(
"userProfile",
`
<div class="profile">
{{component "avatar" src=avatarUrl size="large"}}
{{component "userInfo" name=name email=email}}
</div>
`,
);Registers a simple helper function.
Example:
import { registerHelper } from "./mod.ts";
registerHelper("uppercase", (text: string) => {
return text.toString().toUpperCase();
});
registerHelper("formatDate", (date: Date, format: string) => {
// Custom date formatting logic
return date.toLocaleDateString();
});Registers a block helper that can wrap content.
Example:
import { registerBlockHelper } from "./mod.ts";
registerBlockHelper(
"repeat",
(context: unknown, options: BlockHelperOptions) => {
const count = context as number;
let result = "";
for (let i = 0; i < count; i++) {
result += options.fn({ index: i, value: i + 1 });
}
return result;
},
);Helpers support both variables and string literals:
Blocks define overrideable sections:
Templates can extend other templates that also extend templates:
// Base
registerBaseTemplate(
"layout",
`
<html>
{{#block "head"}}Default head{{/block}}
<body>{{#block "body"}}Default body{{/block}}</body>
</html>
`,
);
// Page template
registerBaseTemplate(
"page",
`
{{extends "layout"}}
{{#block "head"}}<title>{{title}}</title>{{/block}}
{{#block "body"}}<main>{{#block "main"}}Default main{{/block}}</main>{{/block}}
`,
);- Precompile templates when possible
- Use caching - compiled templates are automatically cached
- Avoid complex expressions in conditionals
- Register components and layouts at startup
- Use string literals in helpers for better performance
See MIGRATION_GUIDE.md for detailed migration examples from other template engines.