Templates
Templates are the building blocks of Blaze-NG applications. They define reactive HTML that automatically updates when data changes.
Defining Templates
Templates are defined in .html files using the <template> tag:
The name attribute must be unique across your application. It becomes the key you use to reference the template in JavaScript.
Accessing Templates
In JavaScript/TypeScript, templates are available on the Template object:
import { Template } from '@blaze-ng/core';
// Access by name
const myTemplate = Template.myComponent;
// Check if a template exists
if (Template.myComponent) {
// ...
}Template Data Context
Every template has a data context — an object that provides the default values for {{expressions}}:
// Render with a data context
Blaze.renderWithData(
Template.userCard,
{
name: 'Jane Doe',
email: 'jane@example.com',
role: 'Admin',
},
container,
);Or via inclusion in another template:
Template Composition
Simple Inclusion
Include one template inside another:
Inclusion with Arguments
Pass data to included templates:
Dynamic Templates
Render different templates based on reactive data:
Template.layout.helpers({
currentPage() {
return Router.current().template;
},
});Or use a helper that returns a template:
Template.layout.helpers({
pageTemplate() {
const page = Session.get('currentPage');
return Template[page] || Template.notFound;
},
});Template Body
The <body> section in your HTML is a special template:
<!-- client/main.html -->
<head>
<title>My App</title>
</head>
<body>
<div id="app">{{> App}}</div>
</body>Block Content
Templates can accept block content using Template.contentBlock:
Use it as a block helper:
With else blocks:
toHTML — Server-Side Rendering
Render any template to an HTML string:
// Simple
const html = Blaze.toHTML(Template.myPage);
// With data
const html = Blaze.toHTMLWithData(Template.myPage, {
title: 'Hello',
items: [1, 2, 3],
});This works perfectly for SSR, email templates, or PDF generation.
Best Practices
Keep Templates Small
Each template should represent a single, focused piece of UI. If a template file exceeds 50 lines, consider splitting it.
Use Naming Conventions
Header.html → Template.Header
todoItem.html → Template.todoItem
adminDashboard → Template.adminDashboardCo-locate Files
Keep template, helpers, events, and styles together:
components/
TodoItem/
TodoItem.html
TodoItem.ts
TodoItem.css