Skip to content

Badge ​

A short status label. Corresponds to PHP's app/Component/Badge/BadgeView.blade.php.

Rust ​

rust
use cg_component::{Badge, Color, Component, Size};

Badge::new("Paid").color(Color::Success).icon("check").render();
Badge::new("Draft").size(Size::Sm).rounded(false).render();
MethodDefaultDescription
Badge::new(label)—label takes &str / String (escaped) or Html (output as is)
.color(Color)Neutralcg-badge--{color}
.size(Size)Mdcg-badge--{size}
.icon(name or Icon)noneA string becomes an Sm Icon; you can also pass a configured Icon
.rounded(bool)truecg-badge--rounded
.class(...) / .attribute(...) / .attributes(...)—See Template functions and attributes

Template ​

jinja
{{ cg_badge("Draft", color="warning", icon="warning") }}
{{ cg_badge("New", size="sm", rounded=false, attrs={"class": "ml-2", "title": "Last 7 days"}) }}
ParameterValues
label (first)Text; values from `
colorneutral primary success warning danger info
sizesm md lg
iconA registered icon name
roundedtrue / false
attrsA map; see Template functions and attributes

Output ​

html
<span class="cg-badge cg-badge--success cg-badge--md cg-badge--rounded" data-component="badge">
    <svg class="cg-icon cg-icon--sm" data-component="icon" data-icon="check" viewBox="0 0 24 24" … aria-hidden="true">
        <path d="M5 12.5l4 4L19 6.5" />
    </svg>
    <span class="cg-badge__label">Paid</span>
</span>

(The real output has no line breaks or indentation; it's formatted here for readability.)

  • Root class order: cg-badge, the color, the size, cg-badge--rounded, then your classes.
  • The icon comes before the label and is decorative (aria-hidden="true"); the label provides the text.

With state ​

Badge has no behavior of its own, but it can display a data-state-* value:

rust
Badge::new("0").color(Color::Info).attribute("data-state-text", "matchCount")

data-state-text replaces the root's textContent, wiping out the icon and cg-badge__label along with it. To keep the structure, put the attribute on an element inside the label:

rust
let count = Html::raw(r#"<span data-state-text="matchCount">0</span> matches"#.to_owned());
Badge::new(count).color(Color::Info)

Tests ​

tests/php_parity.rs: badges_match_the_php_overview_snapshot (colors, sizes, icons) and the_default_badge_matches_the_php_catalog_snapshot.