---
weight: 9
---

# Template blocks

Template blocks can be used to customize the types and builders generated by cog.

For example, to automatically position panels on the grid whenever they're added to a dashboard.

To achieve that, cog looks for [template blocks](https://pkg.go.dev/text/template#example-Template-Block) following
a particular naming convention, and renders them.

For types, the following blocks are supported:

* `object_{{ Package }}_{{ ObjectName }}_custom_unmarshal`: allows the definition of a custom unmarshal function for the object.
* `object_{{ Package }}_{{ ObjectName }}_field_{{ FieldName}}_custom_strict_unmarshal`: allows the definition of a custom — strict — unmarshal logic for a field.
* `object_{{ Package }}_{{ ObjectName }}_custom_methods`: allows the definition of custom methods on objects.
* `object_all_custom_methods`: allows the definition of custom methods on all objects.
* `variant_{{ VariantName }}_field_unmarshal`: defines how to unmarshal fields of the given variant.
* `object_variant_{{ VariantName }}`: allows the definition of custom methods for all objects implementing the variant `{{ VariantName }}`.
* `schema_variant_{{ VariantName }}`: allows the definition of custom code for all schemas implementing the variant `{{ VariantName }}`.

For builders, the following blocks are supported:

* `pre_assignment_{{ BuilderName }}_{{ OptionName }}`: will be rendered within an option, before the assignment.
* `post_assignment_{{ BuilderName }}_{{ OptionName }}`: will be rendered within an option, after the assignment.
* `builder_{{ Package }}_{{ BuilderName }}_custom_methods`: allows the definition of custom methods on builders.

For API reference documentation, the following blocks are supported:

* `api_reference_package_{{ Package }}_extra`: additional content that will be rendered on the documentation page for a package.
* `api_reference_object_{{ Package }}_{{ ObjectName }}_extra`: additional content that will be rendered on the documentation page for an object.
* `api_reference_builder_{{ Package }}_{{ BuilderName }}_extra`: additional content that will be rendered on the documentation page for a builder.

For dynamically generating files, the following block is supported:
* `dynamic_files`: in conjunction with the `declareFile $filepath $content` template helper.

!!! note

    The variables components of block names are **case-sensitive**.

## Custom object methods

Custom methods can be added with either:

* `object_{{ Package }}_{{ ObjectName }}_custom_methods`: applies to one object.
* `object_all_custom_methods`: applies to every object.

Both blocks receive `Object` in the template context, so you can use object metadata directly
(for example, `.Object.Name`).

Example:

```gotemplate
{{ define "object_all_custom_methods" }}
func (resource {{ .Object.Name|formatObjectName }}) CustomMethod() string {
	return "custom-for-{{ .Object.Name }}"
}
{{ end }}
```
