Skip to content

Rendering

Rendering might be tricky, but we're trying to make it as simple as possible. By default, we're using `html/template` as a rendering engine. It's a well-known built-in package, so you don't have to learn anything new.

Out of the box we're parsing all templates in root directory with `*.html` glob. You can change this behavior with `TEMPLATE_GLOB` global variable. Don't rely on file names while working with template names, use `define` entry for each your component.

To provide your components with ability to be rendered, you have to do some basic steps. First, you have to nest one of the rendering implementations into your component state (f.e. `rendering.Template`).

go
package main

type ComponentState struct {
	component.Disposable
	rendering.Template // This line allows you to render your component with html/template
}

...

You can customize rendering with providing values to the rendering implementation. If you need to modify these values for the entire project, we recommend looking at the global settings or creating a builder function for rendering object.

go
package main

...

func Component(ctx *component.Context) component.State {
	state := &ComponentState{}
	state.Template.Name = "CustomName" // Set custom template name
	...
}

By default, render handler will use a component name as a template name. So, you have to define a template with the same name as your component (not the filename, but "define" entry).

html
{{ define "Component" }}
	...
{{ end }}

That's enough to be rendered by `rendering.Handler`.

For rendering a nested component, use built-in `template` function. Provide a resolved future object as a template argument in this way. Nested components are not obligated to have rendering implementation if you're using them in this way.

html
<div>{{ template "component" call .Component }}</div>

As an alternative, you can nest rendering implementation (e.g. `rendering.Template`) into your nested component. In this way you can use `render` function to simplify your code. Please, don't use this approach heavily now, as it affects rendering performance.

html
<div>{{ render .Component }}</div>