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")): whattime_zone_optionsoffers andocre time-zonesprints.
Functions§
- class_
names - Space-separated class names whose condition is true (Rails’
class_names/token_list). - current_
page - Whether
urlnames 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) withradiuscharacters 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,
unitfirst:1234.5with"$"becomes$1,234.50,-3becomes-$3.00(Rails’number_to_currency). - number_
to_ human - Three significant digits and a word, Thousand to Quadrillion:
1234567becomes1.23 Million(Rails’number_to_human). - number_
to_ human_ size - A byte count in 1024 steps:
1536becomes1.5 KB(Rails’number_to_human_size, asstorage::human_size). - number_
to_ percentage - Rounds to
precisiondecimals and adds%:12.345with 1 becomes12.3%(Rails’number_to_percentage). - number_
with_ delimiter - Groups the integer part by thousands:
1234567.891becomes1,234,567.891(Rails’number_with_delimiter). - number_
with_ precision - Rounds to
precisiondecimals:3.14159with 2 becomes3.14(Rails’number_with_precision). - progress_
bar - A progress bar with the element id
id: a<progress>atpercent(clamped to 0-100) and alabel(escaped), as HTML. - strftime
- Formats a time in UTC with
strftimedirectives:%b %-d, %YgivesSep 29, 2026. - time_
ago_ in_ words - The time from
valuetonow, in words:about 3 hours(Rails’time_ago_in_words). - time_
zone_ options - The
<option>s of everyTIME_ZONESname,selectedmarked (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.DateTimeFormatwithtimeZone). - word_
wrap - Breaks lines longer than
widthcharacters at spaces (Rails’word_wrap).