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
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\""
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"
Escapes HTML special characters.
Util.escape_html("<script>alert('xss')</script>")
# => "<script>alert('xss')</script>"
Formats bytes as human-readable file size.
Util.filesize(1024) # => "1 KB"
Util.filesize(1048576) # => "1 MB"
Util.filesize(1073741824) # => "1 GB"
Highlights occurrences of a phrase in text.
Util.highlight("Hello World", "World")
# => "Hello <mark>World</mark>"
Converts underscored/camelCase to human-readable text.
Util.humanize("user_name") # => "User name"
Util.humanize("firstName") # => "First name"
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"
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)
Formats a number as a percentage.
Util.percentage(0.756) # => "76%"
Util.percentage(0.756, precision: 1) # => "75.6%"
Pluralizes a word based on count.
Util.pluralize(1, "item") # => "item"
Util.pluralize(2, "item") # => "items"
Util.pluralize(2, "person", "people") # => "people"
Converts newlines to HTML line breaks.
Util.simple_format("Hello\nWorld") # => "Hello<br>World"
Generates a URL-safe slug from text.
Util.slugify("Hello World!") # => "hello-world"
Strips HTML tags from a string.
Util.strip_tags("<p>Hello <b>World</b></p>") # => "Hello World"
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>"
Builds HTML attributes string from a hash.
Util.tag_attributes({class: "btn", id: "submit"})
# => "class=\"btn\" id=\"submit\""
Title-cases a string.
Util.titleize("hello world") # => "Hello World"
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…"
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>"
URL-encodes a string.
Util.url_encode("hello world") # => "hello%20world"