Filters, Functions & Tests

Filters

Filters transform values. Use them with the pipe operator: {{ value | filter_name }}.

String filters

FilterDescriptionExample
upperUppercase{{ "hello" | upper }}HELLO
lowerLowercase{{ "HELLO" | lower }}hello
capitalizeCapitalize first letter{{ "hello world" | capitalize }}Hello world
titleTitle Case{{ "hello world" | title }}Hello World
trimStrip whitespace{{ " hi " | trim }}hi
escapeHTML-escape{{ "<b>" | escape }}&lt;b&gt;
safeMark as safe HTML (no escaping){{ page.content | safe }}
splitSplit into array{{ "a,b,c" | split(pat=",") }}

Collection filters

FilterDescriptionExample
lengthNumber of items{{ items | length }}
firstFirst item{{ items | first }}
lastLast item{{ items | last }}
reverseReverse order{{ items | reverse }}
sortSort ascending{{ items | sort }}
joinJoin into string{{ items | join(sep=", ") }}
sliceSubsequence{{ items | slice(start=1, end=3) }}
mapExtract attribute{{ pages | map(attribute="title") }}
selectattrFilter by attribute{{ pages | selectattr(attribute="extra.featured") }}
rejectattrExclude by attribute{{ pages | rejectattr(attribute="extra.draft") }}
groupbyGroup by attribute{{ pages | groupby(attribute="extra.category") }}

Other filters

FilterDescriptionExample
defaultFallback value{{ x | default(value="none") }}
typeofType name{{ x | typeof }}string
path_segmentsSplit URL path{{ "/a/b/c" | path_segments }}
path_firstFirst path segment{{ "/a/b/c" | path_first }}a
path_parentParent path{{ "/a/b/c" | path_parent }}/a/b/
path_basenameLast path segment{{ "/a/b/c" | path_basename }}c

Functions

Call functions with {{ function_name(arg=value) }}.

FunctionDescriptionExample
get_section(path=...)Get a section by content path{% set blog = get_section(path="blog/_index.md") %}
get_url(path=...)Get URL for a content path{{ get_url(path="blog/post.md") }}
now(format=...)Current date/time{{ now(format="%Y-%m-%d") }}
build(step_name)Run a build step{{ build("git_hash") }}
read(file=...)Read a file's contents{{ read(file="VERSION") }}
throw(message)Abort with error{{ throw("missing required field") }}

get_section example

html
{% set blog = get_section(path="blog/_index.md") %}
<h2>Latest posts</h2>
{% for post in blog.pages %}
    <a href="{{ post.permalink }}">{{ post.title }}</a>
{% endfor %}

Build steps

Build steps are configured in dodeca.styx and executed via the build() function:

styx
build_steps {
    git_hash {
        command (git rev-parse --short HEAD)
    }
}
html
<footer>Built from {{ build("git_hash") }}</footer>

Tests

Tests check conditions in {% if %} blocks. Use with the is keyword.

TestDescriptionExample
definedVariable exists{% if x is defined %}
undefinedVariable doesn't exist{% if x is undefined %}
noneValue is null{% if x is none %}
stringValue is a string{% if x is string %}
numberValue is a number{% if x is number %}
mappingValue is an object{% if x is mapping %}
iterableValue is a sequence{% if x is iterable %}
oddNumber is odd{% if loop.index is odd %}
evenNumber is even{% if loop.index is even %}
divisiblebyDivisible by N{% if x is divisibleby(3) %}
containingContains substring/item{% if path is containing("/blog/") %}
starting_withStarts with prefix{% if path is starting_with("/docs") %}
ending_withEnds with suffix{% if path is ending_with(".html") %}
matchingMatches regex{% if name is matching("^[a-z]+$") %}
eqEqual{% if x is eq(5) %}
neNot equal{% if x is ne(0) %}
lt / gtLess / greater than{% if x is gt(10) %}
le / geLess/greater or equal{% if x is le(100) %}