Skip to content

Template Files

Template files are registered in .slcc or .slcp files using the template_file key. Upon project generation, the template file is rendered to the autogen/ directory in the project using the Jinja2 templating engine. If the template file name has a .jinja or .jinja2 extension, it is stripped.

Components and projects may register variables for use in the Jinja2 template file. All components present in the project contribute variables to the same global pool, which is available to all templates for the given project generation. This is done using the template_contribution key. Variables registered using template contributions are always lists, where each template contribution entry adds one element to the list. If multiple components provide content to the same variable, the list is ordered by the priority given to each contribution from lowest to highest (most negative to most positive). When two contributions have the same priority, the id of the contributing component is used to achieve stable sorting.

Built-in Functionality

In addition to the contributions from components, some macros are made available to templates by the templating engine itself:

  • autogenerated_file - emits a description that the file is generated and what template it was generated from. Takes prefix and suffix arguments that are added to each line of the output, suitable for wrapping the description in comment tags for the target language of the generated file.
  • autogenerated_c_file - equivalent to {{ autogenerated_file | format(prefix='// ', suffix='')}}

Example

Component:

1
2
3
4
5
6
7
8
template_file:
  - path: my_template.h.jinja
template_contribution:
  - name: fish
    value: marlin
  - name: cake
    value:
      type: lie

Template:

1
2
3
4
5
6
7
8
9
{{ autogenerated_c_file }}

{% for f in fish %}
int fish_{{f}}(void);
{% endfor %}

{% for c in cake %}
/* The cake is a {{ c['type'] }} */
{% endfor %}

Output:

1
2
3
4
5
6
7
8
// This file is autogenerated by Silicon Labs SLC.
// The contents of this file will be replaced in their entirety upon regeneration.
//
// Source template file: my_template.h.jinja

int fish_marlin(void);

/* The cake is a lie */

Back to top