Skip to content

Commit

Permalink
Bootstrap setting
Browse files Browse the repository at this point in the history
  • Loading branch information
dyve committed Apr 22, 2024
1 parent cfdd0ce commit a678395
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
{% load django_bootstrap5 %}
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
<html lang="{{ LANGUAGE_CODE|default:'en_us' }}"{% with color_mode='color_mode'|bootstrap_setting %}{% if color_mode %} data-bs-theme="{{ color_mode }}"{% endif %}{% endwith %}>
{% bootstrap_setting 'color_mode' as COLOR_MODE %}
<html lang="{{ LANGUAGE_CODE|default:'en_us' }}"{% if COLOR_MODE %} data-bs-theme="{{ COLOR_MODE }}"{% endif %}>
<head>

<!-- Required meta tags -->
Expand Down
44 changes: 38 additions & 6 deletions src/django_bootstrap5/templatetags/django_bootstrap5.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
from ..components import render_alert, render_button
from ..core import css_url, get_bootstrap_setting, javascript_url, theme_url
from ..css import _css_class_list, merge_css_classes
from ..forms import render_field, render_form, render_form_errors, render_formset, render_formset_errors, render_label
from ..forms import (
render_field,
render_form,
render_form_errors,
render_formset,
render_formset_errors,
render_label,
)
from ..html import render_link_tag, render_script_tag
from ..size import get_size_class
from ..utils import render_template_file, url_replace_param
Expand All @@ -26,8 +33,8 @@
register = template.Library()


@register.filter
def bootstrap_setting(value):
@register.filter(name="bootstrap_setting")
def bootstrap_setting_filter(value):
"""
Return django-bootstrap5 setting for use in in a template.
Expand All @@ -36,6 +43,16 @@ def bootstrap_setting(value):
return get_bootstrap_setting(value)


@register.simple_tag(name="bootstrap_setting")
def bootstrap_setting_tag(value):
"""
Return django-bootstrap5 setting for use in in a template.
Please consider this tag private, do not use it in your own templates.
"""
return get_bootstrap_setting(value)


@register.simple_tag
def bootstrap_server_side_validation_class(widget):
"""
Expand All @@ -47,7 +64,13 @@ def bootstrap_server_side_validation_class(widget):
css_classes = _css_class_list([widget["attrs"]["class"]])
except KeyError:
return ""
return " ".join([css_class for css_class in css_classes if css_class in ["is-valid", "is-invalid"]])
return " ".join(
[
css_class
for css_class in css_classes
if css_class in ["is-valid", "is-invalid"]
]
)


@register.simple_tag
Expand Down Expand Up @@ -739,7 +762,9 @@ def get_pagination_context(
"""Generate Bootstrap pagination context from a page object."""
pages_to_show = int(pages_to_show)
if pages_to_show < 1:
raise ValueError(f"Pagination pages_to_show should be a positive integer, you specified {pages_to_show}.")
raise ValueError(
f"Pagination pages_to_show should be a positive integer, you specified {pages_to_show}."
)

num_pages = page.paginator.num_pages
current_page = page.number
Expand Down Expand Up @@ -775,7 +800,14 @@ def get_pagination_context(
if extra:
params.update(parse_qs(extra))
url = urlunparse(
[parts.scheme, parts.netloc, parts.path, parts.params, urlencode(params, doseq=True), parts.fragment]
[
parts.scheme,
parts.netloc,
parts.path,
parts.params,
urlencode(params, doseq=True),
parts.fragment,
]
)

pagination_css_classes = ["pagination"]
Expand Down

0 comments on commit a678395

Please sign in to comment.