Template Helpers

Azu provides built-in template helpers for common web development tasks like building forms, generating links, formatting dates, and handling internationalization.

Quick Reference

{# Forms #}
{{ form_tag("/users", method="post") }}
  {{ csrf_field() }}
  {{ text_field("user", "name", required=true) }}
  {{ submit_button("Create") }}
{{ end_form() }}

{# Links #}
{{ link_to("Home", "/", class="nav-link") }}
{{ button_to("Delete", "/posts/1", method="delete") }}

{# Assets #}
{{ stylesheet_tag("app.css") }}
{{ javascript_tag("app.js", defer=true) }}
{{ image_tag("logo.png", alt="Logo") }}

{# i18n #}
{{ t("welcome.title") }}
{{ t("greeting", name=user.name) }}
{{ created_at | l("date.short") }}

{# Numbers #}
{{ price | currency("$") }}
{{ 1234567 | number_with_delimiter }}

{# Dates #}
{{ created_at | time_ago }}
{{ date | date_format("%Y-%m-%d") }}

Form Helpers

form_tag

Opens a form with CSRF protection:

Parameters:

Parameter
Type
Default
Description

action

string

""

Form action URL

method

string

"post"

HTTP method (get, post, put, patch, delete)

class

string

nil

CSS class

id

string

nil

Element ID

enctype

string

nil

Form encoding type

multipart

bool

false

Set to true for file uploads

data

hash

nil

Data attributes

onsubmit

string

nil

JavaScript onsubmit handler

Non-standard methods (put, patch, delete) automatically add a hidden _method field.

end_form

Closes a form tag:

csrf_field

Generates a hidden CSRF token input:

csrf_meta

Generates a CSRF meta tag for JavaScript use:

text_field

Generates a text input:

Parameters:

Parameter
Type
Default
Description

object

string

""

Object name (e.g., "user")

attribute

string

""

Attribute name (e.g., "name")

value

any

nil

Input value

placeholder

string

nil

Placeholder text

class

string

nil

CSS class

id

string

nil

Override generated ID

required

bool

false

Mark as required

disabled

bool

false

Disable input

readonly

bool

false

Read-only input

autofocus

bool

false

Auto-focus on load

maxlength

int

nil

Maximum length

minlength

int

nil

Minimum length

pattern

string

nil

Validation pattern

data

hash

nil

Data attributes

email_field

Generates an email input:

password_field

Generates a password input:

number_field

Generates a number input:

Additional parameters: min, max, step

textarea

Generates a textarea:

Additional parameters: rows, cols

hidden_field

Generates a hidden input:

checkbox

Generates a checkbox with hidden unchecked value:

Additional parameters: checked, label, unchecked_value (default "0")

radio_button

Generates a radio button:

Additional parameters: value, checked, label

select_field

Generates a select dropdown:

Parameters:

Parameter
Type
Default
Description

options

array

[]

Array of {value, label} objects

selected

string

nil

Pre-selected value

include_blank

string

nil

Blank option text

multiple

bool

false

Allow multiple selection

label_tag

Generates a label element:

submit_button

Generates a submit button:


URL Helpers

Generates an anchor tag:

Parameters:

Parameter
Type
Default
Description

text

string

""

Link text

href

string

""

URL

class

string

nil

CSS class

id

string

nil

Element ID

target

string

nil

Target (_blank, _self, etc.)

rel

string

nil

Rel attribute

title

string

nil

Title attribute

data

hash

nil

Data attributes

Links with target="_blank" automatically add rel="noopener noreferrer".

button_to

Generates a form with a submit button (for non-GET actions):

Parameters:

Parameter
Type
Default
Description

text

string

""

Button text

href

string

""

Action URL

method

string

"post"

HTTP method

class

string

nil

CSS class

confirm

string

nil

Confirmation message

disabled

bool

false

Disable button

data

hash

nil

Data attributes

mail_to

Generates a mailto link:

Parameters:

Parameter
Type
Default
Description

email

string

""

Email address

text

string

nil

Link text (defaults to email)

subject

string

nil

Email subject

body

string

nil

Email body

cc

string

nil

CC addresses

bcc

string

nil

BCC addresses

class

string

nil

CSS class

id

string

nil

Element ID

current_path

Returns the current request path:

current_url

Returns the full current URL:

is_current_page

Filter that checks if a path matches the current page:

active_class

Filter that returns a class name if the path is active:

Parameters:

Parameter
Type
Default
Description

class_name

string

"active"

Class to return if active

inactive_class

string

""

Class to return if inactive

exact

bool

true

Exact match vs prefix match

back_url

Returns the referer URL or a fallback:


Type-Safe Endpoint Helpers

Azu automatically generates type-safe URL and form helpers from your endpoint definitions. The HTTP method is part of the helper name, making it impossible to confuse which action will be performed.

How They're Generated

When you define an endpoint with an HTTP method, Azu auto-generates helpers:

This generates the following helpers:

Endpoint
Generated Helpers

UsersEndpoint.get "/users"

link_to_get_users()

UserEndpoint.get "/users/:id"

link_to_get_user(id=...)

CreateUserEndpoint.post "/users"

link_to_post_create_user(), form_for_post_create_user()

UpdateUserEndpoint.put "/users/:id"

link_to_put_update_user(id=...), form_for_put_update_user(id=...)

DeleteUserEndpoint.delete "/users/:id"

link_to_delete_delete_user(id=...), form_for_delete_delete_user(id=...), button_to_delete_delete_user(id=...)

Generates anchor tags for any endpoint:

Parameters:

Parameter
Type
Default
Description

text

string

path

Link text

id

string

nil

Path parameter value (for :id routes)

class

string

nil

CSS class

target

string

nil

Target (_blank, _self, etc.)

data

hash

nil

Data attributes

params

hash

nil

Custom URL query parameters

With custom query parameters:

form_for_{method}_{resource}

Generates form opening tags for non-GET endpoints. PUT, PATCH, and DELETE methods automatically include a hidden _method field:

Parameters:

Parameter
Type
Default
Description

id

string

nil

Path parameter value (for :id routes)

class

string

nil

CSS class

enctype

string

nil

Form encoding type

data

hash

nil

Data attributes

params

hash

nil

Custom params as hidden fields

With custom hidden fields:

button_to_delete_{resource}

Generates a complete delete form with a submit button (only for DELETE endpoints):

Parameters:

Parameter
Type
Default
Description

text

string

"Delete"

Button text

id

string

nil

Path parameter value (for :id routes)

class

string

nil

CSS class for the button

confirm

string

nil

JavaScript confirmation message

data

hash

nil

Data attributes

params

hash

nil

Custom params as hidden fields

With custom hidden fields:

Helper Naming Convention

The helper name is derived from the endpoint class name:

Class Name
Helper Resource Name

UsersEndpoint

users

UserEndpoint

user

CreateUserEndpoint

create_user

Admin::UsersEndpoint

admin_users

Api::V1::UserEndpoint

api_v1_user

Benefits

  1. Intuitive: link_to_get_users clearly indicates a GET request

  2. Hard to confuse: The HTTP method is in the name, not a parameter

  3. Type-safe: Helpers are generated at compile-time from your endpoints

  4. Consistent: Same pattern works for all endpoints


Asset Helpers

asset_path

Filter that returns the asset URL:

image_tag

Generates an image element:

Parameters:

Parameter
Type
Default
Description

src

string

""

Image source

alt

string

""

Alt text

width

int

nil

Width

height

int

nil

Height

class

string

nil

CSS class

id

string

nil

Element ID

loading

string

nil

Loading strategy (lazy, eager)

javascript_tag

Generates a script element:

Parameters:

Parameter
Type
Default
Description

src

string

""

Script source

defer

bool

false

Defer loading

async

bool

false

Async loading

type

string

nil

Script type

id

string

nil

Element ID

stylesheet_tag

Generates a link stylesheet element:

Parameters:

Parameter
Type
Default
Description

href

string

""

Stylesheet URL

media

string

"all"

Media type

id

string

nil

Element ID

favicon_tag

Generates a favicon link:


Date Helpers

time_ago

Filter that formats a time as relative past:

relative_time

Filter that formats time relative to now (past or future):

date_format

Filter that formats a date with a strftime pattern:

time_tag

Function that generates a time element:

distance_of_time

Filter that converts seconds to human-readable duration:


Number Helpers

currency

Filter that formats a number as currency:

Parameters:

Parameter
Type
Default
Description

symbol

string

"$"

Currency symbol

precision

int

2

Decimal places

delimiter

string

","

Thousands delimiter

separator

string

"."

Decimal separator

number_with_delimiter

Filter that adds thousands separators:

percentage

Filter that formats a decimal as percentage:

filesize

Filter that formats bytes as human-readable:

number_to_human

Filter that formats large numbers with words:


HTML Helpers

safe_html

Filter that marks content as safe (no escaping):

Warning: Only use with trusted content to prevent XSS.

simple_format

Filter that converts newlines to <br> and wraps in paragraphs:

highlight

Filter that highlights occurrences of a phrase:

truncate_html

Filter that truncates HTML while preserving structure:

strip_tags

Filter that removes HTML tags:

word_wrap

Filter that wraps text at a specified width:

Filter that converts URLs and emails to links:

content_tag

Function that generates an HTML tag:


i18n Helpers

t (Translate)

Function that translates a key:

Parameters:

Parameter
Type
Default
Description

key

string

required

Translation key

default

string

nil

Fallback if key is missing

count

int

nil

For pluralization

**options

hash

{}

Interpolation values

Pluralization:

Define translations with zero, one, other keys:

l (Localize)

Filter that localizes dates using translation formats:

current_locale

Function that returns the current locale:

available_locales

Function that returns available locales:

locale_name

Filter that returns display name for a locale code:

pluralize

Function for simple pluralization:


Component Helpers

For use with Azu's Spark real-time component system.

spark_tag

Generates the Spark JavaScript bootstrap:

render_component

Renders a Spark live component:

Live Attribute Filters

Add Spark live attributes to elements:


Complete Example

See Also

Last updated

Was this helpful?