Backstage templating is powered by Nunjucks. The basics:
Templating Filters
The filter is a critical mechanism for the rendering of Nunjucks templates, providing a means of transforming values in a familiar piped fashion. Templating filters are functions that help you transform data, extract specific information, and perform various operations in Scaffolder templates.
Built-in
Backstage provides out of the box the following set of "built-in" templating filters (to create your own custom filters, look to the section Custom Filter hereafter):
parseRepoUrl
The parseRepoUrl filter parses a repository URL into its constituent parts:
owner, repository name (repo), etc.
Usage Example:
- id: log
name: Parse Repo URL
action: debug:log
input:
message: ${{ parameters.repoUrl | parseRepoUrl }}
- Input:
github.com?repo=backstage&owner=backstage - Output: "RepoSpec" (see parseRepoUrl)
parseEntityRef
The parseEntityRef filter allows you to extract different parts of
an entity reference, such as the kind, namespace, and name.
Usage example
- Without context
- id: log
name: Parse Entity Reference
action: debug:log
input:
message: ${{ parameters.owner | parseEntityRef }}
- Input:
group:techdocs - Output: CompoundEntityRef
- With context
- id: log
name: Parse Entity Reference
action: debug:log
input:
message: ${{ parameters.owner | parseEntityRef({ defaultKind:"group", defaultNamespace:"another-namespace" }) }}
- Input:
techdocs - Output: CompoundEntityRef
pick
The pick filter allows you to select a specific property (e.g. kind, namespace, name) from an object.
Usage Example
- id: log
name: Pick
action: debug:log
input:
message: ${{ parameters.owner | parseEntityRef | pick('name') }}
- Input:
{ kind: 'Group', namespace: 'default', name: 'techdocs' } - Output:
techdocs
projectSlug
The projectSlug filter generates a project slug from a repository URL.
Usage Example
- id: log
name: Project Slug
action: debug:log
input:
message: ${{ parameters.repoUrl | projectSlug }}
- Input:
github.com?repo=backstage&owner=backstage - Output:
backstage/backstage
Templating Globals
In addition to its powerful filtering functionality, the Nunjucks engine allows access from the template expression context to specified globally-accessible references. Backstage propagates this capability via the scaffolder backend plugin, which we shall soon see in action.
Customizing the templating environment
Custom plugins make it possible to install your own templating extensions, which may be any combination of filters, global functions and global values. With the new backend you would use a scaffolder plugin module for this; later we will demonstrate the analogous approach with the old backend.
Streamlining Templating Extension Module Creation with the Backstage CLI
The creation of a "template environment customization" module in Backstage can be accelerated using the Backstage CLI.
Start by using the yarn backstage-cli new command to generate a scaffolder module. This command sets up the necessary boilerplate code, providing a smooth start:
$ yarn backstage-cli new
? What do you want to create?
frontend-plugin - A new frontend plugin
backend-plugin - A new backend plugin
❯ backend-plugin-module - A new backend module that extends an existing backend plugin
plugin-web-library - A new web library plugin package
plugin-node-library - A new Node.js library plugin package
plugin-common-library - A new isomorphic common plugin package
web-library - A library package, exporting shared functionality for web environments
When prompted, use the arrow keys to select the option to generate a backend-plugin-module.
Since we want to extend the Scaffolder backend, enter scaffolder when prompted for the ID of the plugin to extend.
Next, enter the ID (name) of your module. This will be appended to the scaffolder-backend-module- prefix. The CLI will then generate the required files and directory structure, for example:
? Enter the ID of the plugin [required] scaffolder
? Enter the ID of the module [required] foo-bar
templating plugins/scaffolder-backend-module-foo-bar ✔
backend adding @internal/plugin-scaffolder-backend-module-foo-bar ✔
executing yarn install ✔
executing yarn lint --fix ✔