Skip to main content

Module helpers

Module helpers 

Source
Expand description

View helpers: numbers, dates and text formatted like Rails’ number_to_currency, time_ago_in_words, excerpt…

Plain functions returning String, usable anywhere (a JSON field, an email, a log line). Templates use them as askama filters through filters ({{ post.price|number_to_currency("$") }}, feature html).

Number helpers take any value whose text is a number (i64, f64, a String from D1…) and return any other text unchanged. Time helpers take Unix seconds (i64, as now returns) or the text D1 stores (2026-09-29 14:05:00, 2026-09-29T14:05:00Z, 2026-09-29, with an optional +02:00 offset), read as UTC when there is no offset, and return other text unchanged. All are plain string work: microseconds of CPU, no binding call. English only; I18n has the localized versions (l, number, currency, time_ago_in_words…).

Constants§

TIME_ZONES
IANA time zone names, as browsers list them (Intl.supportedValuesOf("timeZone")): what time_zone_options offers and ocre time-zones prints.

Functions§

class_names
Space-separated class names whose condition is true (Rails’ class_names / token_list).
current_page
Whether url names the page being shown (Rails’ current_page?), for “you are here” links.
distance_of_time_in_words
The time between two times, in words: 5 minutes, 2 days (Rails’ distance_of_time_in_words).
excerpt
The first match of phrase (case-insensitive) with radius characters around it, ... where cut (Rails’ excerpt).
highlight
The text, HTML-escaped, with each match of phrase (case-insensitive) in <mark> (Rails’ highlight).
number_to_currency
Two decimals, thousands grouped, unit first: 1234.5 with "$" becomes $1,234.50, -3 becomes -$3.00 (Rails’ number_to_currency).
number_to_human
Three significant digits and a word, Thousand to Quadrillion: 1234567 becomes 1.23 Million (Rails’ number_to_human).
number_to_human_size
A byte count in 1024 steps: 1536 becomes 1.5 KB (Rails’ number_to_human_size, as storage::human_size).
number_to_percentage
Rounds to precision decimals and adds %: 12.345 with 1 becomes 12.3% (Rails’ number_to_percentage).
number_with_delimiter
Groups the integer part by thousands: 1234567.891 becomes 1,234,567.891 (Rails’ number_with_delimiter).
number_with_precision
Rounds to precision decimals: 3.14159 with 2 becomes 3.14 (Rails’ number_with_precision).
progress_bar
A progress bar with the element id id: a <progress> at percent (clamped to 0-100) and a label (escaped), as HTML.
strftime
Formats a time in UTC with strftime directives: %b %-d, %Y gives Sep 29, 2026.
time_ago_in_words
The time from value to now, in words: about 3 hours (Rails’ time_ago_in_words).
time_zone_options
The <option>s of every TIME_ZONES name, selected marked (Rails’ time_zone_select): write them in a <select>, and store the IANA name the form sends. Ocre’s own time helpers stay UTC; convert for display in the browser (Intl.DateTimeFormat with timeZone).
word_wrap
Breaks lines longer than width characters at spaces (Rails’ word_wrap).