This page covers the three render calls, the attribute bag, titles and accessible labels, and the errors a render can return.
The calls
| Call | PHP equivalent | Use |
|---|---|---|
icons.svg(name, class, attributes) | svg($name, $class, $attributes) | Class and attributes |
icons.svg_with(name, attributes) | svg($name, $attributes) | Attributes only |
icons.icon(name) | svg($name) | No per-call defaults |
Each returns Result<Svg>. Svg::to_html() returns the markup, and Svg also implements Display:
let svg = icons.svg("heroicon-o-home", "size-6", Attributes::new())?;
let html: String = svg.to_html();
to_html() doesn't change the Svg; calling it twice returns the same string.
Attributes
Attributes is an ordered bag. Names keep the order you set them in, and setting a name again replaces
its value in place:
use oxblade_icons::Attributes;
let attrs = Attributes::new()
.set("stroke-width", "1.5")
.set("width", 24)
.set("hidden", true) // renders hidden="1", like PHP's true
.set("data-off", false) // renders data-off="", like PHP's false
.flag("x-cloak"); // a bare attribute, like a PHP array value without a key
Shortcuts:
| Method | Sets |
|---|---|
size(px) | width and height |
label(text) | role="img" and aria-label |
decorative() | aria-hidden="true" |
without(name) | removes a named value |
Values may be strings, integers and booleans. Double quotes in values become "; other characters
are written as they are, as in PHP. Attribute names must start with a letter or _ and contain only
letters, digits, _, :, . and -. Names that start with on, in any case, are rejected, so a bag
can't add an event handler.
Set attribute names in your code. Values may come from users, but names should not.
As in PHP, the attributes are written at the start of every <svg tag in the icon, before the SVG's own
attributes, and the SVG is not parsed. When you pass an attribute the SVG already has, such as
stroke-width, the tag contains it twice; browsers use the first, which is yours.
After resolving
Svg has fluent setters, like PHP's svg('…')->width(24):
let svg = icons.icon("lucide-map")?.attr("width", 24)?.flag("x-cloak")?;
PHP turns the method name into the attribute name (strokeWidth() becomes stroke-width); in Rust you
pass the attribute name directly. Svg::name(), contents() and attributes() return the resolved icon
name, the original SVG and the prepared attributes.
Titles and labels
A title attribute adds a <title> element inside every <svg> element and role="img" on the outer
one, as in Blade Icons:
let svg = icons.svg_with("heroicon-c-home", Attributes::new().set("title", "Home & away"))?;
// <svg title="Home & away" … role="img"><title>Home & away</title>…</svg>
Plain strings have < and > escaped inside <title>, so a title taken from a user can't add markup.
PHP doesn't escape here. When a title is markup you wrote yourself and you need PHP's exact output, wrap
it in Trusted:
use oxblade_icons::Trusted;
Attributes::new().set("title", Trusted("Home <small>(1)</small>"));
Trusted follows PHP byte for byte, including PHP's replacement rules: $0, \0 and ${0} in a trusted
title insert the matched <svg …> tag, and $1 to $99 insert nothing. Never wrap text that comes from
a request, a database field or another user-controlled source.
Trusted also works for other attribute values passed to Svg::attr, where it keeps double quotes as
they are, like PHP's fluent methods.
Errors
| Error | When |
|---|---|
NotFound(name) | The icon and every fallback are missing |
InvalidName(name) | An icon, set or prefix name has characters outside letters, digits, - and _ (and . between parts of an icon name) |
InvalidAttribute(name) | An attribute name is malformed or starts with on |
DuplicateSet(name) | Two sets share a name or prefix (from build()) |
UnknownFallback(name) | A configured fallback doesn't resolve (from build()) |
Deferred | The defer attribute is set to anything but false; deferred icons aren't supported |
Error implements std::error::Error and Display, and is #[non_exhaustive].
Supported source markup
The renderer accepts a passive subset with common shapes, groups, text, gradients and filters. It rejects embedded HTML, script and style elements, malformed tags, and markup inside existing titles or descriptions. Keep control of every registered SVG and its links. This validation is for safe interpolation, not a sanitizer for uploads.