diff --git a/docs/djlint/best_practices.rst b/docs/djlint/best_practices.rst
index 9d2e536..46b91b8 100644
--- a/docs/djlint/best_practices.rst
+++ b/docs/djlint/best_practices.rst
@@ -20,10 +20,10 @@ This pattern is not recommended:
content
^ space here
-Spaceless Conditional Attributes
---------------------------------
+``format_attribute_template_tags`` and Spaceless Conditional Attributes
+-----------------------------------------------------------------------
-Conditional attributes should use spaceless tags, for example ``{% if a -%}`` in nunjuck and jinja, to remove spaces inside the.
+If ``format_attribute_template_tags`` option is enabled, conditional attributes should use spaceless tags, for example ``{% if a -%}`` in nunjuck and jinja, or ``{% spaceless %}{% if a %}{% endspaceless %}`` in django, to remove spaces inside the.
djLint will format long attributes onto multiple lines, and the whitespace saved inside of attributes could break your code.
diff --git a/docs/djlint/changelog.rst b/docs/djlint/changelog.rst
index 054ceeb..f3ae32e 100644
--- a/docs/djlint/changelog.rst
+++ b/docs/djlint/changelog.rst
@@ -1,6 +1,10 @@
Changelog
=========
+next release
+------------
+- Added config option ``format_attribute_template_tags`` as opt-in for template tag formatting inside of attributes
+
0.6.6
-----
- Big fixes
diff --git a/docs/djlint/configuration.rst b/docs/djlint/configuration.rst
index 3260725..518e1dc 100644
--- a/docs/djlint/configuration.rst
+++ b/docs/djlint/configuration.rst
@@ -160,3 +160,24 @@ Usage:
.. code:: ini
use_gitignore = True
+
+format_attribute_template_tags
+------------------------------
+
+Formatter will attempt to format template syntax inside of tag attributes. Disabled by default.
+
+Usage:
+
+.. code:: ini
+
+ format_attribute_template_tags=true
+
+For example, with this option enabled, the following html will be acceptable:
+
+.. code:: html
+
+
diff --git a/src/djlint/formatter/attributes.py b/src/djlint/formatter/attributes.py
index 667bd71..9d754b7 100644
--- a/src/djlint/formatter/attributes.py
+++ b/src/djlint/formatter/attributes.py
@@ -124,7 +124,9 @@ def format_template_tags(config: Config, attributes: str) -> str:
)
base_indent_space = base_indent * " "
- indented += f"\n{leading_space}{base_indent_space}{tmp}"
+
+ if tmp.strip() != "":
+ indented += f"\n{leading_space}{base_indent_space}{tmp}"
end_text = re.findall(re.compile(r"[\"']$", re.M), line.strip())
@@ -260,7 +262,8 @@ def format_attributes(config: Config, match: re.match) -> str:
attributes = f"{leading_space}{tag}{attributes}{close}"
# format template tags
- attributes = format_template_tags(config, attributes)
+ if config.format_attribute_template_tags:
+ attributes = format_template_tags(config, attributes)
# format styles
func = partial(format_style)
diff --git a/src/djlint/settings.py b/src/djlint/settings.py
index c7fba60..dcf680f 100644
--- a/src/djlint/settings.py
+++ b/src/djlint/settings.py
@@ -180,6 +180,10 @@ class Config:
build_custom_blocks(djlint_settings.get("custom_blocks")) or ""
)
+ self.format_attribute_template_tags = djlint_settings.get(
+ "format_attribute_template_tags", False
+ )
+
# ignore is based on input and also profile
self.ignore: str = str(ignore or djlint_settings.get("ignore", ""))
diff --git a/tests/config_format_attribute_template_tags/html-one.html b/tests/config_format_attribute_template_tags/html-one.html
new file mode 100644
index 0000000..dcdc8f4
--- /dev/null
+++ b/tests/config_format_attribute_template_tags/html-one.html
@@ -0,0 +1,77 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+{% block body %}
+
+{% endblock body %}
+