module

Azu::Helpers::Util

Utility functions for template helpers.

These methods provide common functionality used across multiple helper categories like HTML generation, number formatting, and string manipulation.

Instance methods

build_html_attributes_from_crinja(args : Crinja::Arguments, exclude : Array(String) = [] of String) : String

Builds HTML attributes string from Crinja arguments.

This method extracts attribute values from Crinja function arguments and builds an HTML attribute string, excluding specified keys.

# In a Crinja function:
attrs = Util.build_html_attributes_from_crinja(arguments, ["text", "id"])
# => " class=\"btn\" target=\"_blank\""
Source
currency(number : Number, symbol : String = "$", precision : Int32 = 2, delimiter : String = ",", separator : String = ".") : String

Formats a number as currency.

Util.currency(1234.5)                 # => "$1,234.50"
Util.currency(1234.5, symbol: "€")    # => "€1,234.50"
Util.currency(1234.567, precision: 3) # => "$1,234.567"
Source
escape_html(text : String) : String

Escapes HTML special characters.

Util.escape_html("<script>alert('xss')</script>")
# => "&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;"
Source
filesize(bytes : Number, precision : Int32 = 1) : String

Formats bytes as human-readable file size.

Util.filesize(1024)       # => "1 KB"
Util.filesize(1048576)    # => "1 MB"
Util.filesize(1073741824) # => "1 GB"
Source
highlight(text : String, phrase : String, highlighter : String = "<mark>\\0</mark>") : String

Highlights occurrences of a phrase in text.

Util.highlight("Hello World", "World")
# => "Hello <mark>World</mark>"
Source
humanize(text : String) : String

Converts underscored/camelCase to human-readable text.

Util.humanize("user_name") # => "User name"
Util.humanize("firstName") # => "First name"
Source
number_with_delimiter(number : Number, delimiter : String = ",", separator : String = ".") : String

Formats a number with thousands delimiter.

Util.number_with_delimiter(1234567)      # => "1,234,567"
Util.number_with_delimiter(1234.56)      # => "1,234.56"
Util.number_with_delimiter(1234567, ".") # => "1.234.567"
Source
parse_time(value) : Time

Parses a time value from various formats.

Util.parse_time(Time.utc)     # => Time
Util.parse_time("2024-01-15") # => Time
Util.parse_time(1705276800)   # => Time (from unix timestamp)
Source
percentage(number : Number, precision : Int32 = 0) : String

Formats a number as a percentage.

Util.percentage(0.756)               # => "76%"
Util.percentage(0.756, precision: 1) # => "75.6%"
Source
pluralize(count : Number, singular : String, plural : String | Nil = nil) : String

Pluralizes a word based on count.

Util.pluralize(1, "item")             # => "item"
Util.pluralize(2, "item")             # => "items"
Util.pluralize(2, "person", "people") # => "people"
Source
simple_format(text : String) : String

Converts newlines to HTML line breaks.

Util.simple_format("Hello\nWorld") # => "Hello<br>World"
Source
slugify(text : String) : String

Generates a URL-safe slug from text.

Util.slugify("Hello World!") # => "hello-world"
Source
strip_tags(html : String) : String

Strips HTML tags from a string.

Util.strip_tags("<p>Hello <b>World</b></p>") # => "Hello World"
Source
tag(name : String, attrs : Hash(String, String) = {} of String => String, content : String | Nil = nil, self_closing : Bool = false) : String

Builds an HTML tag.

Util.tag("input", {type: "text", name: "email"})
# => "<input type=\"text\" name=\"email\" />"

Util.tag("div", {class: "container"}, "Content")
# => "<div class=\"container\">Content</div>"
Source
tag_attributes(attrs : Hash(String, String)) : String

Builds HTML attributes string from a hash.

Util.tag_attributes({class: "btn", id: "submit"})
# => "class=\"btn\" id=\"submit\""
Source
titleize(text : String) : String

Title-cases a string.

Util.titleize("hello world") # => "Hello World"
Source
truncate(text : String, length : Int32 = 100, omission : String = "...") : String

Truncates a string to the specified length.

Util.truncate("Hello World", 8)      # => "Hello..."
Util.truncate("Hello", 10)           # => "Hello"
Util.truncate("Hello World", 8, "…") # => "Hello W…"
Source
truncate_html(html : String, length : Int32 = 100, omission : String = "...") : String

Truncates HTML while preserving tag structure.

This is a simplified implementation that tries to keep tags balanced.

Util.truncate_html("<p>Hello <b>World</b></p>", 10)
# => "<p>Hello <b>W...</b></p>"
Source
url_encode(text : String) : String

URL-encodes a string.

Util.url_encode("hello world") # => "hello%20world"
Source
void_tag(name : String, attrs : Hash(String, String) = {} of String => String) : String

Builds a self-closing HTML tag.

Util.void_tag("input", {type: "text"})
# => "<input type=\"text\" />"
Source