Fluent API
When you call swarm_icon() or $manager->get(), you get back an Icon object. It’s immutable: every method returns a new instance, so you can chain calls without worrying about side effects.
echo swarm_icon('tabler:home') ->size(24) ->class('text-blue-500') ->strokeWidth(1.5);Styling methods
Section titled “Styling methods”size()
Section titled “size()”Sets both width and height at once. Accepts numbers or CSS units:
echo swarm_icon('tabler:home')->size(32); // width="32" height="32"echo swarm_icon('tabler:home')->size('2rem'); // width="2rem" height="2rem"class()
Section titled “class()”Appends CSS classes. Never replaces existing ones (they accumulate):
echo swarm_icon('tabler:home')->class('text-blue-500');echo swarm_icon('tabler:home')->class(['w-6', 'h-6', 'text-blue-500']);
// Multiple calls stack:echo swarm_icon('tabler:home')->class('w-6')->class('text-blue');// → class="w-6 text-blue"fill() and stroke()
Section titled “fill() and stroke()”Set the fill and stroke attributes:
echo swarm_icon('mdi:account')->fill('currentColor');echo swarm_icon('tabler:home')->stroke('#3b82f6');strokeWidth()
Section titled “strokeWidth()”Sets stroke-width:
echo swarm_icon('tabler:home')->strokeWidth(1.5);attr()
Section titled “attr()”For anything else, attr() sets arbitrary attributes. By default it merges with existing ones:
echo swarm_icon('tabler:home')->attr(['data-icon' => 'home', 'id' => 'main-icon']);Pass merge: false to replace all attributes instead of merging:
echo swarm_icon('tabler:home')->attr(['class' => 'only-this'], merge: false);Setting a value to null removes that attribute:
echo swarm_icon('tabler:home')->attr(['aria-hidden' => null]);Immutability
Section titled “Immutability”This is worth emphasizing: the original icon is never modified. Every method returns a new copy:
$home = swarm_icon('tabler:home');$large = $home->size(48);$blue = $home->class('text-blue');
echo $home; // original: no size, no classecho $large; // width="48" height="48"echo $blue; // class="text-blue"This makes icons safe to pass around, store in variables, and reuse without surprises.
Reading attributes back
Section titled “Reading attributes back”Sometimes you need to inspect an icon rather than render it:
$icon = swarm_icon('tabler:home')->size(24)->class('icon');
$icon->getContent(); // inner SVG (without the <svg> wrapper)$icon->getAttributes(); // ['viewBox' => '...', 'width' => '24', ...]$icon->getAttribute('class'); // 'icon'$icon->getAttribute('data-x', 'none'); // 'none' (not set, returns default)$icon->hasAttribute('class'); // trueOutput
Section titled “Output”Echo an icon directly: it implements Stringable:
echo swarm_icon('tabler:home');Or call toHtml() explicitly when you need the string without echoing:
$html = swarm_icon('tabler:home')->size(24)->toHtml();Both produce the same full <svg> tag with all attributes and content.
Creating icons directly
Section titled “Creating icons directly”Most of the time you’ll use swarm_icon() or $manager->get(). But you can also create icons from raw SVG data:
use Frostybee\SwarmIcons\Icon;
// From a file on disk$icon = Icon::fromFile('/path/to/icon.svg');
// From a raw SVG string$icon = Icon::fromString('<svg viewBox="0 0 24 24"><path d="..."/></svg>');
// From Iconify API JSON data (handles hFlip, vFlip, rotate)$icon = Icon::fromIconifyData([ 'body' => '<path d="..."/>', 'width' => 24, 'height' => 24,]);Serialization
Section titled “Serialization”Icons support PHP’s native serialize() / unserialize(), which is how the file cache stores them on disk:
$serialized = serialize($icon);$restored = unserialize($serialized);echo $restored; // works exactly like the originalIn Twig
Section titled “In Twig”Use get_icon() to get the Icon object and chain methods in your template:
{% set star = get_icon('tabler:star') %}{{ star.size(32).class('text-yellow-500') }}
{{ get_icon('tabler:arrow-right').fill('currentColor').strokeWidth(1.5) }}See also
Section titled “See also”- Core Concepts: how providers, prefixes, and attributes work together
- Defaults & Rendering: global and prefix-level attribute defaults