Skip to content

Commit

Permalink
fix(dx): resolve_class (#18417) (#18421)
Browse files Browse the repository at this point in the history
* fix(resolve_class): ignore False values

* fix: allow multiple arguments

* fix: remove unnecessary condition

* test: resolve_class

(cherry picked from commit 4f65085)

Co-authored-by: Faris Ansari <netchampfaris@users.noreply.github.com>
  • Loading branch information
mergify[bot] and netchampfaris committed Oct 17, 2022
1 parent 63e3c61 commit c47de0d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
16 changes: 16 additions & 0 deletions frappe/tests/test_website.py
Expand Up @@ -317,6 +317,22 @@ def test_metatags(self):
self.assertIn('<meta name="title" content="Test Title Metatag">', content)
self.assertIn('<meta name="description" content="Test Description for Metatag">', content)

def test_resolve_class(self):
from frappe.utils.jinja_globals import resolve_class

context = frappe._dict(primary=True)
self.assertEqual(resolve_class("test"), "test")
self.assertEqual(resolve_class("test", "test-2"), "test test-2")
self.assertEqual(resolve_class("test", {"test-2": False, "test-3": True}), "test test-3")
self.assertEqual(
resolve_class(["test1", "test2", context.primary and "primary"]), "test1 test2 primary"
)

content = '<a class="{{ resolve_class("btn btn-default", primary and "btn-primary") }}">Test</a>'
self.assertEqual(
frappe.render_template(content, context), '<a class="btn btn-default btn-primary">Test</a>'
)


def set_home_page_hook(key, value):
from frappe import hooks
Expand Down
10 changes: 6 additions & 4 deletions frappe/utils/jinja_globals.py
Expand Up @@ -2,12 +2,14 @@
# License: MIT. See LICENSE


def resolve_class(classes):
def resolve_class(*classes):
if classes and len(classes) == 1:
classes = classes[0]

if classes is None:
return ""

if isinstance(classes, str):
return classes
if classes is False:
return ""

if isinstance(classes, (list, tuple)):
return " ".join(resolve_class(c) for c in classes).strip()
Expand Down

0 comments on commit c47de0d

Please sign in to comment.