Skip to content

Commit bc725e0

Browse files
authored
Merge pull request #94 from jchristgit/add-include-js-flag
Allow omitting JS resources on templatetag call
2 parents 3e3eac8 + 649a4f2 commit bc725e0

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ theme by providing a key that matches the regex `\w+_variables` (e.g. `alt_varia
102102
stylesheets will then be generated using the variables at that key.
103103

104104
To use these stylesheets in a template, pass the theme name to the `{% bulma %}` tag either as a
105-
string `{% bulma 'alt' %}` or as a template variable `{% bulma theme %}`.
105+
string `{% bulma 'alt' %}` or as a template variable `{% bulma theme %}`. When calling the `bulma` template
106+
more than once in the same document (for example for implementing a dark theme switch), you will want to pass `include_js=False` to
107+
at least one of these to prevent duplicate loading of JavaScript resources.
106108

107109
#### Extensions
108110

django_simple_bulma/templatetags/django_simple_bulma.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,20 @@
1212

1313

1414
@register.simple_tag
15-
def bulma(theme: str = "") -> SafeString:
16-
"""Build static files required for Bulma."""
15+
def bulma(theme: str = "", *, include_js: bool = True) -> SafeString:
16+
"""Build static files required for Bulma.
17+
18+
Parameters:
19+
theme:
20+
CSS theme to load. If the given theme can not be found, a warning
21+
will be logged and the library will fall back to the default theme.
22+
23+
Keyword arguments:
24+
include_js:
25+
Whether to include directives to load Bulma's JavaScript resources
26+
in the result. Useful to prevent duplicate loading of JS when
27+
calling this tag more than once on the same resource.
28+
"""
1729
from ..utils import (
1830
get_js_files,
1931
logger,
@@ -36,8 +48,9 @@ def bulma(theme: str = "") -> SafeString:
3648
]
3749

3850
# Build html to include all the js files required.
39-
for js_file in map(static, get_js_files()):
40-
html.append(f'<script defer type="text/javascript" src="{js_file}"></script>')
51+
if include_js:
52+
for js_file in map(static, get_js_files()):
53+
html.append(f'<script defer type="text/javascript" src="{js_file}"></script>')
4154

4255
return mark_safe("\n".join(html))
4356

0 commit comments

Comments
 (0)