diff --git a/pyproject.toml b/pyproject.toml
index 81e3754..0d7aaf1 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -153,6 +153,11 @@ indent-style = "space"
profile = "django"
indent = 2
single_attribute_per_line = true
+# {% cf_ui_class %}...{% endcf_ui_class %} is a custom block tag (cf_ui.py)
+# that lets a cotton theme partial write its class="..." if/elif chain one
+# branch per line — Django has no {%- -%} trim markers, so without this the
+# tag looks like an unmatched end tag (T038) to djlint's own parser.
+custom_blocks = "cf_ui_class"
# Both extensions are formatted. The Jinja tree is passed `--profile=jinja`
# on its own hook invocation; this file-level setting cannot vary per path.
extension = "html"
diff --git a/src/cf_ui/templates/cotton/_themes/bootstrap/button.html b/src/cf_ui/templates/cotton/_themes/bootstrap/button.html
index 33b003a..f066148 100644
--- a/src/cf_ui/templates/cotton/_themes/bootstrap/button.html
+++ b/src/cf_ui/templates/cotton/_themes/bootstrap/button.html
@@ -1 +1,34 @@
-{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="btn{% if variant == 'primary' %} btn-primary{% elif variant == 'secondary' %} btn-secondary{% elif variant == 'success' %} btn-success{% elif variant == 'warning' %} btn-warning{% elif variant == 'danger' %} btn-danger{% elif variant == 'info' %} btn-info{% elif variant == 'neutral' %} btn-light{% endif %}{% if size == 'small' %} btn-sm{% elif size == 'large' %} btn-lg{% endif %}{% if state == 'disabled' %} disabled{% endif %}{% if full_width %} w-100{% endif %}{% if class %} {{ class }}{% endif %}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{% if state == 'loading' %} {% endif %}{{ slot }}{{ cf_tag }}>{% endwith %}
+{% load cf_ui %}
+{% cf_ui_class as cf_class %}
+ btn
+ {% if variant == 'primary' %}
+ btn-primary
+ {% elif variant == 'secondary' %}
+ btn-secondary
+ {% elif variant == 'success' %}
+ btn-success
+ {% elif variant == 'warning' %}
+ btn-warning
+ {% elif variant == 'danger' %}
+ btn-danger
+ {% elif variant == 'info' %}
+ btn-info
+ {% elif variant == 'neutral' %}
+ btn-light
+ {% endif %}
+ {% if size == 'small' %}
+ btn-sm
+ {% elif size == 'large' %}
+ btn-lg
+ {% endif %}
+ {% if state == 'disabled' %}
+ disabled
+ {% endif %}
+ {% if full_width %}
+ w-100
+ {% endif %}
+ {% if class %}
+ {{ class }}
+ {% endif %}
+{% endcf_ui_class %}
+{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="{{ cf_class }}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{% if state == 'loading' %} {% endif %}{{ slot }}{{ cf_tag }}>{% endwith %}
diff --git a/src/cf_ui/templates/cotton/_themes/bulma/button.html b/src/cf_ui/templates/cotton/_themes/bulma/button.html
index 27026bf..ae47981 100644
--- a/src/cf_ui/templates/cotton/_themes/bulma/button.html
+++ b/src/cf_ui/templates/cotton/_themes/bulma/button.html
@@ -1 +1,32 @@
-{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="button{% if variant == 'primary' %} is-primary{% elif variant == 'secondary' %} is-link{% elif variant == 'success' %} is-success{% elif variant == 'warning' %} is-warning{% elif variant == 'danger' %} is-danger{% elif variant == 'info' %} is-info{% endif %}{% if size == 'small' %} is-small{% elif size == 'large' %} is-large{% endif %}{% if state == 'loading' %} is-loading{% endif %}{% if full_width %} is-fullwidth{% endif %}{% if class %} {{ class }}{% endif %}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{{ slot }}{{ cf_tag }}>{% endwith %}
+{% load cf_ui %}
+{% cf_ui_class as cf_class %}
+ button
+ {% if variant == 'primary' %}
+ is-primary
+ {% elif variant == 'secondary' %}
+ is-link
+ {% elif variant == 'success' %}
+ is-success
+ {% elif variant == 'warning' %}
+ is-warning
+ {% elif variant == 'danger' %}
+ is-danger
+ {% elif variant == 'info' %}
+ is-info
+ {% endif %}
+ {% if size == 'small' %}
+ is-small
+ {% elif size == 'large' %}
+ is-large
+ {% endif %}
+ {% if state == 'loading' %}
+ is-loading
+ {% endif %}
+ {% if full_width %}
+ is-fullwidth
+ {% endif %}
+ {% if class %}
+ {{ class }}
+ {% endif %}
+{% endcf_ui_class %}
+{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="{{ cf_class }}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{{ slot }}{{ cf_tag }}>{% endwith %}
diff --git a/src/cf_ui/templates/cotton/_themes/daisy/button.html b/src/cf_ui/templates/cotton/_themes/daisy/button.html
index f804323..0758230 100644
--- a/src/cf_ui/templates/cotton/_themes/daisy/button.html
+++ b/src/cf_ui/templates/cotton/_themes/daisy/button.html
@@ -1 +1,34 @@
-{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="btn{% if variant == 'primary' %} btn-primary{% elif variant == 'secondary' %} btn-secondary{% elif variant == 'success' %} btn-success{% elif variant == 'warning' %} btn-warning{% elif variant == 'danger' %} btn-error{% elif variant == 'info' %} btn-info{% elif variant == 'neutral' %} btn-neutral{% endif %}{% if size == 'small' %} btn-sm{% elif size == 'large' %} btn-lg{% endif %}{% if state == 'disabled' %} btn-disabled{% endif %}{% if full_width %} btn-block{% endif %}{% if class %} {{ class }}{% endif %}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{% if state == 'loading' %} {% endif %}{{ slot }}{{ cf_tag }}>{% endwith %}
+{% load cf_ui %}
+{% cf_ui_class as cf_class %}
+ btn
+ {% if variant == 'primary' %}
+ btn-primary
+ {% elif variant == 'secondary' %}
+ btn-secondary
+ {% elif variant == 'success' %}
+ btn-success
+ {% elif variant == 'warning' %}
+ btn-warning
+ {% elif variant == 'danger' %}
+ btn-error
+ {% elif variant == 'info' %}
+ btn-info
+ {% elif variant == 'neutral' %}
+ btn-neutral
+ {% endif %}
+ {% if size == 'small' %}
+ btn-sm
+ {% elif size == 'large' %}
+ btn-lg
+ {% endif %}
+ {% if state == 'disabled' %}
+ btn-disabled
+ {% endif %}
+ {% if full_width %}
+ btn-block
+ {% endif %}
+ {% if class %}
+ {{ class }}
+ {% endif %}
+{% endcf_ui_class %}
+{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="{{ cf_class }}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{% if state == 'loading' %} {% endif %}{{ slot }}{{ cf_tag }}>{% endwith %}
diff --git a/src/cf_ui/templates/cotton/_themes/fomantic/button.html b/src/cf_ui/templates/cotton/_themes/fomantic/button.html
index 6191bec..db47898 100644
--- a/src/cf_ui/templates/cotton/_themes/fomantic/button.html
+++ b/src/cf_ui/templates/cotton/_themes/fomantic/button.html
@@ -1 +1,34 @@
-{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="ui button{% if variant == 'primary' %} primary{% elif variant == 'secondary' %} secondary{% elif variant == 'success' %} positive{% elif variant == 'warning' %} yellow{% elif variant == 'danger' %} negative{% elif variant == 'info' %} teal{% endif %}{% if size == 'small' %} small{% elif size == 'large' %} large{% endif %}{% if state == 'loading' %} loading{% elif state == 'disabled' %} disabled{% endif %}{% if full_width %} fluid{% endif %}{% if class %} {{ class }}{% endif %}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{{ slot }}{{ cf_tag }}>{% endwith %}
+{% load cf_ui %}
+{% cf_ui_class as cf_class %}
+ ui button
+ {% if variant == 'primary' %}
+ primary
+ {% elif variant == 'secondary' %}
+ secondary
+ {% elif variant == 'success' %}
+ positive
+ {% elif variant == 'warning' %}
+ yellow
+ {% elif variant == 'danger' %}
+ negative
+ {% elif variant == 'info' %}
+ teal
+ {% endif %}
+ {% if size == 'small' %}
+ small
+ {% elif size == 'large' %}
+ large
+ {% endif %}
+ {% if state == 'loading' %}
+ loading
+ {% elif state == 'disabled' %}
+ disabled
+ {% endif %}
+ {% if full_width %}
+ fluid
+ {% endif %}
+ {% if class %}
+ {{ class }}
+ {% endif %}
+{% endcf_ui_class %}
+{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="{{ cf_class }}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{{ slot }}{{ cf_tag }}>{% endwith %}
diff --git a/src/cf_ui/templates/cotton/_themes/foundation/button.html b/src/cf_ui/templates/cotton/_themes/foundation/button.html
index 656dba9..c3d0a16 100644
--- a/src/cf_ui/templates/cotton/_themes/foundation/button.html
+++ b/src/cf_ui/templates/cotton/_themes/foundation/button.html
@@ -1 +1,32 @@
-{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="button{% if variant == 'primary' %} primary{% elif variant == 'secondary' %} secondary{% elif variant == 'success' %} success{% elif variant == 'warning' %} warning{% elif variant == 'danger' %} alert{% elif variant == 'info' %} secondary{% endif %}{% if size == 'small' %} small{% elif size == 'large' %} large{% endif %}{% if state == 'disabled' %} disabled{% endif %}{% if full_width %} expanded{% endif %}{% if class %} {{ class }}{% endif %}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{{ slot }}{{ cf_tag }}>{% endwith %}
+{% load cf_ui %}
+{% cf_ui_class as cf_class %}
+ button
+ {% if variant == 'primary' %}
+ primary
+ {% elif variant == 'secondary' %}
+ secondary
+ {% elif variant == 'success' %}
+ success
+ {% elif variant == 'warning' %}
+ warning
+ {% elif variant == 'danger' %}
+ alert
+ {% elif variant == 'info' %}
+ secondary
+ {% endif %}
+ {% if size == 'small' %}
+ small
+ {% elif size == 'large' %}
+ large
+ {% endif %}
+ {% if state == 'disabled' %}
+ disabled
+ {% endif %}
+ {% if full_width %}
+ expanded
+ {% endif %}
+ {% if class %}
+ {{ class }}
+ {% endif %}
+{% endcf_ui_class %}
+{% with cf_tag=href|yesno:"a,button" %}<{{ cf_tag }} class="{{ cf_class }}"{% if href %}{% if state == 'disabled' %} role="link" aria-disabled="true"{% else %} href="{{ href }}"{% endif %}{% else %} type="{{ type }}"{% if state == 'disabled' %} disabled{% endif %}{% endif %}>{{ slot }}{{ cf_tag }}>{% endwith %}
diff --git a/src/cf_ui/templatetags/cf_ui.py b/src/cf_ui/templatetags/cf_ui.py
index 34e2f27..1daf64b 100644
--- a/src/cf_ui/templatetags/cf_ui.py
+++ b/src/cf_ui/templatetags/cf_ui.py
@@ -1,3 +1,5 @@
+import re
+
from django import template
from django.conf import settings
from django.templatetags.static import static
@@ -10,6 +12,8 @@
register = template.Library()
+_WHITESPACE_RE = re.compile(r"\s+")
+
_CDN_CSS = {
"bulma": "https://cdn.jsdelivr.net/npm/bulma@{v}/css/bulma.min.css",
"bootstrap": "https://cdn.jsdelivr.net/npm/bootstrap@{v}/dist/css/bootstrap.min.css",
@@ -136,6 +140,59 @@ def cf_ui_validate(component: str, **axes) -> str:
return validate_primitive(component, **axes)
+class ClassCaptureNode(template.Node):
+ def __init__(self, nodelist, target_var=None):
+ self.nodelist = nodelist
+ self.target_var = target_var
+
+ def render(self, context) -> str:
+ collapsed = _WHITESPACE_RE.sub(" ", self.nodelist.render(context)).strip()
+ if self.target_var:
+ context[self.target_var] = mark_safe(collapsed)
+ return ""
+ return mark_safe(collapsed)
+
+
+@register.tag("cf_ui_class")
+def cf_ui_class(parser, token):
+ """Collapse a multi-line ``{% if %}`` class chain to a single-space string.
+
+ Django has no Jinja-style ``{%- -%}`` trim markers, so a chained
+ ``{% if %}...{% elif %}...{% endif %}`` class attribute has to stay on one
+ physical line — a literal newline anywhere in the block becomes literal
+ whitespace in the rendered ``class`` attribute. This tag lets the same
+ chain be written readably, one branch per line, by rendering its nodelist
+ normally (inheriting ambient autoescape — a hostile ``{{ class }}`` is
+ escaped exactly as it already was) and then collapsing every run of
+ whitespace between tokens to one space and stripping the ends::
+
+