Math & Data Functions

Mathematical operations and data file manipulation

3 min read 507 words

Mathematical operations for calculations in templates.

percentage

Calculate percentage with optional decimal places.

{{ completed | percentage(total_tasks) }}      {# "75%" #}
{{ score | percentage(max_score, 2) }}         {# "87.50%" #}

Parameters:

  • part: Part value
  • total: Total value
  • decimals: Number of decimal places (default: 0)

times

Multiply value by multiplier.

{{ price | times(1.1) }}      {# Add 10% tax #}
{{ count | times(5) }}        {# Multiply by 5 #}

divided_by

Divide value by divisor. Returns 0 if divisor is 0.

{{ total | divided_by(count) }}      {# Average #}
{{ seconds | divided_by(60) }}       {# Convert to minutes #}

ceil

Round up to nearest integer.

{{ 4.2 | ceil }}   {# 5 #}
{{ 4.9 | ceil }}   {# 5 #}

floor

Round down to nearest integer.

{{ 4.2 | floor }}  {# 4 #}
{{ 4.9 | floor }}  {# 4 #}

round

Round to specified decimal places.

{{ 4.567 | round }}      {# 5 #}
{{ 4.567 | round(2) }}   {# 4.57 #}
{{ 4.567 | round(1) }}   {# 4.6 #}

Data Functions

Functions for loading and manipulating data files.

get_data

Load data from JSON or YAML file. Returns empty dict if file not found.

{% let authors = get_data('data/authors.json') %}
{% for author in authors %}
  {{ author.name }}
{% end %}

{% let config = get_data('data/config.yaml') %}

jsonify

Convert data to JSON string.

{{ data | jsonify }}           {# Compact JSON #}
{{ data | jsonify(2) }}        {# Pretty-printed with indent #}

merge

Merge two dictionaries. Second dict takes precedence.

{% let config = defaults | merge(custom_config) %}
{% let shallow = dict1 | merge(dict2, deep=false) %}

has_key

Check if dictionary has a key.

{% if data | has_key('author') %}
  {{ data.author }}
{% end %}

get_nested

Access nested data using dot notation.

{{ data | get_nested('user.profile.name') }}
{{ data | get_nested('user.email', 'no-email') }}   {# With default #}

keys

Get dictionary keys as list.

{% for key in data | keys %}
  {{ key }}
{% end %}

values

Get dictionary values as list.

{% for value in data | values %}
  {{ value }}
{% end %}

items

Get dictionary items as list of (key, value) tuples.

{% for key, value in data | items %}
  {{ key }}: {{ value }}
{% end %}

File Functions

Functions for reading files and checking file existence.

read_file

Read file contents as string.

{% let license = read_file('LICENSE') %}
{{ license }}

{% let readme = read_file('docs/README.md') %}

file_exists

Check if file exists.

{% if file_exists('custom.css') %}
  <link rel="stylesheet" href="{{ asset_url('custom.css') }}">
{% end %}

file_size

Get human-readable file size.

{{ file_size('downloads/manual.pdf') }}   {# "2.3 MB" #}
{{ file_size('images/hero.png') }}        {# "145.2 KB" #}