Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Filters And Functions

All of the builtin functionalities for Tera are available.

On top of that, Sketch adds some extra filters and functions.

Functions

  • uuid (generates a v4 UUID)

Filters

Strings

  • capture(regex=REGEX) (matches a regex once and returns the named capture groups)
  • capture_many(regex=REGEX) (matches a regex repetitively and returns the list of named capture groups)
  • semver (parses a cargo-style semver and returns the segments)
  • matches_semver(target=TARGET) (checks if a cargo-style semver matches a target)
  • strip_prefix(prefix=PREFIX) (strips a prefix from a string, if present)
  • strip_suffix(suffix=SUFFIX) (strips a suffix from a string, if present)

Filesystem

  • basename (gets the basename of a directory/file)
  • parent_dir (gets the parent directory of a directory/file)
  • is_file (checks if a path is a file)
  • is_dir (checks if a path is a directory)
  • is_absolute (checks if a path is absolute)
  • is_relative (checks if a path is relative)
  • absolute (makes a path absolute)
  • relative(from=PATH) (returns the relative path between two paths)
  • read_dir (returns the list of the files contained in a directory and its subdirectories)
  • glob(pattern=GLOB) (returns the glob matching entries in a directory and its subdirectories)
  • matches_glob(pattern=GLOB) (checks if a path matches a glob pattern)

Serialization

  • to_yaml (serializes input into yaml)
  • to_toml (serializes input into prettified toml)

Examples

Template:

{# Using special variables -#}

Current arch is: {{ sketch_arch }}
Current os is: {{ sketch_os }}
Current os family is: {{ sketch_os_family }}
Is unix: {{ sketch_is_unix }}

{# Functions and filters -#}

A random uuid is: {{ uuid() }}

{# Generating a random alphanumeric string -#}
A random string is: {{ uuid() | truncate(length=6, end="") }}

{# Generating a random number -#}
A random number is: {{ get_random(start=100000, end=9999999) }}

Now is {{ now(utc=true) }}

{# Assigning an env to a variable -#}
{%- set greeting = get_env(name="GREETING") -%}

{#- Manipulating data -#}
{%- set segments = greeting | split(pat=",") -%}
First segment is: {{ segments[0] }}
Second segment is: {{ segments[1] }}

{% set some_path = "mydir/myfile" -%}
Basename is: {{ some_path | basename }}
Parent dir is: {{ some_path | parent_dir }}

{% set dir = "../sketch" -%}

{% if dir | is_dir -%}
It's a dir!
{% endif -%}

{% set file = "Cargo.toml" -%}

{% if file | is_file -%}
It's a file!
{%- endif %}

Matches glob: {{ file | matches_glob(pattern="**/*.toml") }}

{%- set captures = file | capture(regex="(?<path>.*)\.(?<extension>\w+)") %}

Path is: {{ captures['path'] }}
Extension is: {{ captures['extension'] }}

In yaml form:
{{ captures | to_yaml }}

In toml form:
{{ captures | to_toml }}

{% set words = "They're taking the hobbits to Isengard" | capture_many(regex="(?<word>[\w']+)") -%}

{% for capture in words -%}
{{ capture['word'] -}} {% if not loop.last %}{{ " " }}{% endif -%}
{%- endfor -%}!

{% set version = "v0.1.0" -%}

{% set segments = version | semver -%}
Major is: {{ segments['major'] }}
Minor is: {{ segments['minor'] }}
Patch is: {{ segments['patch'] }}

Version matches >=0.1.0: {{ version | matches_semver(target=">=0.1.0") }}
Version matches >=0.2.0: {{ version | matches_semver(target=">=0.2.0") }}

To camelCase: {{ "my_var" | camel }}
To snake_case: {{ "myVar" | snake }}
To SCREAMING_CASE: {{ "myVar" | upper_snake }}
To PascalCase: {{ "myVar" | pascal }}

Luke, I am your {{ "grandfather" | strip_prefix(prefix="grand") }}!

{% set dir = "../examples/templating/templates" -%}

{% for entry in dir | glob(pattern="example.j2") -%}
Entry: {{ entry }}
{% endfor %}

Cmd:

GREETING="hello,world" sketch render --template example.j2 --stdout

Output:

Current arch is: x86_64
Current os is: linux
Current os family is: unix
Is unix: true

A random uuid is: fd61daf5-83bf-46c6-afe3-5fcac0d6212c

A random string is: 0de4ed

A random number is: 3574820

Now is 2025-10-27T19:13:47.625250487+00:00

First segment is: hello
Second segment is: world

Basename is: myfile
Parent dir is: mydir

It's a dir!
It's a file!

Matches glob: true

Path is: Cargo
Extension is: toml

In yaml form:
path: Cargo
extension: toml


In toml form:
path = "Cargo"
extension = "toml"


They're taking the hobbits to Isengard!

Major is: 0
Minor is: 1
Patch is: 0

Version matches >=0.1.0: true
Version matches >=0.2.0: false

To camelCase: myVar
To snake_case: my_var
To SCREAMING_CASE: MY_VAR
To PascalCase: MyVar

Luke, I am your father!

Entry: example.j2