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();| Method | Default | Description |
|---|---|---|
Badge::new(label) | — | label takes &str / String (escaped) or Html (output as is) |
.color(Color) | Neutral | cg-badge--{color} |
.size(Size) | Md | cg-badge--{size} |
.icon(name or Icon) | none | A string becomes an Sm Icon; you can also pass a configured Icon |
.rounded(bool) | true | cg-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"}) }}| Parameter | Values |
|---|---|
label (first) | Text; values from ` |
color | neutral primary success warning danger info |
size | sm md lg |
icon | A registered icon name |
rounded | true / false |
attrs | A 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.