Templates, SPA & i18n
Server-rendered views with a built-in SPA runtime: link clicks are intercepted, partials are swapped in, and the URL updates โ with zero client-side framework.
te := breeze.NewTemplateEngine(breeze.TemplateConfig{ViewsDir: "views"})
router.Handle(breeze.GET, "/", func(ctx *breeze.Context) error {
return te.RenderView(ctx, "home", map[string]any{"Name": "World"})
}) Rendering
| Function | Purpose |
|---|---|
te.RenderView(ctx, viewName, data) | render a full view (layout + page) |
ctx.Render(engine, viewName, data) | same, called from the context |
te.RenderComponent(ctx, componentName, data) | render a reusable component |
router.Fragment(...) | register a route that renders a partial for SPA re-render |
router.View(...) | register a route bound to a view |
te.RenderJSON(ctx) | render the page's data as JSON, for the SPA runtime |
Layouts and reusable components compose normally; EnableReRender wires a
template engine into the router's SPA support so a navigation swaps a
fragment instead of reloading the page.
Partial re-render, no client framework
The SPA runtime is inlined JavaScript Breeze generates and serves alongside
the page (breezeRuntime()), not a dependency you install. Clicking a link
that matches a registered fragment route intercepts the navigation, fetches
the partial, swaps it in, and updates the URL with the History API โ the
rest of the page, including anything a script initialized, is left alone.
Development mode
The engine's parse cache can be disabled in development so edited templates
are re-parsed on the next request rather than requiring a restart โ
configure this via TemplateConfig.
i18n
// locales/en.json: {"home": {"greeting": "Hello, %{name}!"}}
i18n, _ := breeze.NewI18n(breeze.I18nConfig{Dir: "locales", DefaultLocale: "en"})
router.Use(middleware.LocaleMiddleware(i18n)) In a template:
{{t "home.greeting" "name" .User.Name}} In a handler:
ctx.T("home.greeting", "name", ctx.User.Name)
ctx.Locale() // the resolved locale for this request
ctx.SetLocale("fr") // override it LocaleMiddleware resolves the request locale from ?lang=, then the breeze_locale cookie, then the Accept-Language header, and attaches it
to the context for the template engine โ see Middleware Reference.
Locale files
Nested JSON is flattened into dotted keys (home.greeting) at load time. i18n.NegotiateLocale(acceptLanguage) parses an Accept-Language header
against the configured locales and returns the best match; Reload() re-reads locale files from disk, which LocaleMiddleware calls
automatically in development mode.
Pluralization
A translation key can carry a count argument; i18n.T looks for a
count-aware form when one of the passed arguments is numeric and a plural
variant exists for the key, falling back to the singular form otherwise.
Configuration
| Field | Meaning |
|---|---|
TemplateConfig.ViewsDir | root directory views and layouts are loaded from |
I18nConfig.Dir | directory of locale JSON files |
I18nConfig.DefaultLocale | fallback when no locale can be resolved |