Templates#
The top-level templates option describes a few key fields, such as url which
are evaluated by nunjucks.
checks are also evaluated as nunjucks templates.
Additional templates can be defined and imported for reuse as blocks or
macros in other templates or checks.
Context#
Each template gets an object of this form:
{
"config": { "forms": { "a-form-key": {} } },
"data": { "a-form-key": { "some-data-data": "from_form" } }
}
Markdown#
Most non-url templates are rendered as Markdown.
Much of GitHub flavored Markdown is supported, but not platform-specific features
like magic #{issue} transforms and mermaid fenced code blocks. Indeed, no syntax
highlighting is supported, so generally any fence info will be discarded.
Copy Code#
All pre tags (generated with triple ticks, tildes, etc) will be rendered with a copy
button.
This helps for URL-based workflows that don’t allow for populating key parameters such
as GitLab’s /new/ URL, or otherwise complex ones (GitHub’s /edit/).
In this case, it is recommended to provide a below_{form} template
which shows the file content, with narrative describing how to copy the code and what to
do with it.
Special Templates#
A few well-known template names and patterns are used globally.
url#
The templates.url field should generate a valid URL. All whitespace should be escaped
(e.g. use " " | urlencode to get %20), as any remaining will be removed.
submit_target#
The target for the submit_button. If an empty string, the default behavior
of replacing the current page will be used.
use
_blankto open a new browser tabuse the name of an
<iframe name="some-iframe">to replace thatiframe’s content
download_filename#
A filename to suggest for data: URLs when the submit_button is shown.
above_{form}#
A markdown message to show above a form, where form is a key from the root forms
object.
below_{form}#
A markdown message to show below a form, where form is a key from the root forms
object.
checks#
On each change of any form, each member of checks is evaluated, then has leading and
trailing whitespace removed. If the remaining string is non-empty, the check is
considered failed, and the result rendered as markdown and show in the checks section
at the end of the form.
This is useful for implementing cross-cutting constraints that cannot be captured in JSON schema, such as validating unique property values in arrays, or values matching between two, unrelated schema.
{
"checks": {
"Unique names": [
"{% for name, things in data.thing-list.things | default([]) | groupby('name') %}",
"{% set dupes = things | length %}",
"{% if dupes > 1 %}",
"- [ ] {{ dupes }} things have the name _'{{ name }}'_",
"{% endif %}",
"{% endfor %}"
]
}
}
For import purposes, checks are nested under the checks/ path, so must use ../ to
access other templates.
{
"templates": {
"common": [
"{% macro foo %}",
"{% endmacro %}"
]
},
"checks": {
"ok": [
"{% import '../common' %}",
"{{ foo() }}"
]
}
}
Filters#
In addition to the built-in filters and the pythonic jinja2
compatibility layer, some custom filters are available by default, while
format-specific can be lazily loaded.
filter |
note |
|
|---|---|---|
|
encode a string as |
|
|
get the file name from a Data URL |
|
|
get the MIME type from a Data URL |
|
|
build an object from |
|
|
recursively remove |
|
|
get schema validation errors |
|
|
|
parse JSON string |
|
|
build a Data URL for a JSON file |
|
|
build a JSON string. options: |
|
|
parse a TOML string |
|
|
build a Data URL for a TOML file |
|
|
build a TOML string |
|
|
parse a YAML string options: see |
|
|
build a Data URL for a YAML file |
|
|
build a YAML string |
|
|
create a Data URL for a zip archive: see |
Format Filters#
As urljsf already provides support for loading JSON, TOML, YAML as needed, the
parse/serialize functions of the underlying JS libraries may also be enabled for
templates, and lazily loaded before use.
Similarly, .zip files are a very common, low-barrier way to handle trees of files, but
not needed for every case.
To load all of them:
{
"nunjucks": {
"filters": ["toml", "json", "yaml", "zip"]
}
}