module

Azu::Helpers

Template helpers provide a powerful DSL for extending Crinja templates with custom filters, functions, and global variables.

Quick Start

# Define a custom filter
Azu::Helpers.filter :shout do
  target.to_s.upcase + "!"
end

# Define a function with arguments
Azu::Helpers.function({name: "World"}, :greet) do
  "Hello, #{arguments["name"]}!"
end

# Define a global variable
Azu::Helpers.global :app_name, "My Application"

Template Usage

{{ "hello" | shout }}      {# HELLO! #}
{{ greet(name="Alice") }}  {# Hello, Alice! #}
{{ app_name }}             {# My Application #}

Constants

VERSION = "1.0.0"

Class methods

initialize!

Initialize and register all built-in helpers.

This is automatically called when Azu starts, but can be called manually if needed.

Source
reset!

Reset all registered helpers.

Useful for testing.

Source

Macros

filter(defaults = nil, name = nil, &block)

Registers a custom Crinja filter.

Filters transform values using the pipe syntax: {{ value | filter_name }}.

Simple Filter

Azu::Helpers.filter :uppercase do
  target.to_s.upcase
end

Filter with Arguments

Azu::Helpers.filter({precision: 2}, :round_number) do
  target.to_f.round(arguments["precision"].to_i)
end

Inside the block, you have access to:

  • target - The value being filtered
  • arguments - Named arguments passed to the filter
  • env - The Crinja environment
Source
function(defaults = nil, name = nil, &block)

Registers a custom Crinja function.

Functions are called directly in templates: {{ function_name(args) }}.

Simple Function

Azu::Helpers.function :current_year do
  Time.utc.year.to_s
end

Function with Arguments

Azu::Helpers.function({name: "World"}, :greet) do
  "Hello, #{arguments["name"]}!"
end

Inside the block, you have access to:

  • arguments - Named arguments passed to the function
  • env - The Crinja environment
Source
global(name, value)

Registers a global template variable.

Global variables are accessible directly in templates.

Azu::Helpers.global :app_name, "My Application"
Azu::Helpers.global :version, "1.0.0"

In templates:

<title>{{ app_name }} v{{ version }}</title>
Source
test(defaults = nil, name = nil, &block)

Registers a Crinja test.

Tests are used with is syntax: {% if value is test_name %}.

Azu::Helpers.test :blank do
  target.to_s.blank?
end

In templates:

{% if name is blank %}
  <p>No name provided</p>
{% endif %}
Source

Nested types