Skip to content

Commit 5000765

Browse files
committed
for simplicity, use a Django setting to set site title
Also, using a Django setting allows us to avoid CSS pseudo-selector issues
1 parent 6d0c580 commit 5000765

File tree

5 files changed

+29
-30
lines changed

5 files changed

+29
-30
lines changed

pattern_library/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
# after the main bundle to override default styles.
4141
# Example: "css/pattern-library-custom.css"
4242
"CUSTOM_CSS": None,
43+
# SITE_TITLE allows users to customize the pattern library title displayed in the header
44+
"SITE_TITLE": "Django Pattern Library",
4345
}
4446

4547

pattern_library/static/pattern_library/src/scss/_config.scss

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
// Configuration with support for runtime customization via CSS custom properties
22

33
// Default SCSS variables for compile-time usage (needed for Sass functions)
4-
$site-title: 'Django Pattern Library' !default;
54
$color-primary: #34b2b2 !default;
65
$family-primary: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif !default;
76

87
// CSS Custom Properties for runtime customization
9-
// These will override the compiled styles where used directly (not in Sass functions)
10-
:root {
11-
--site-title: #{$site-title};
8+
html, :root {
129
--color-primary: #{$color-primary};
1310
--family-primary: #{$family-primary};
1411
}

pattern_library/static/pattern_library/src/scss/layout/_header.scss

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
letter-spacing: 1px;
1818
font-size: 16px;
1919
margin-left: 15px;
20-
21-
&::after {
22-
content: var(--site-title);
23-
}
20+
margin-top: 0;
21+
margin-bottom: 0;
2422

2523
@media only screen and (min-width: 600px) {
2624
font-size: 22px;

pattern_library/templates/pattern_library/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</a>
2222
<h1 class="header__title">
2323
<span class="sr-only">Pattern Library</span>
24-
{# Set in _config.scss #}
24+
{% pattern_library_site_title %}
2525
</h1>
2626
</header>
2727
<aside class="sidebar">

pattern_library/templatetags/pattern_library_tags.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22
from django.utils.safestring import mark_safe
33
from django.templatetags.static import static
44
from pattern_library import get_setting
5-
import os
65

76
register = template.Library()
87

98

109
@register.simple_tag
1110
def pattern_library_custom_css():
1211
"""
13-
Include custom CSS file for pattern library customization.
12+
Include custom CSS for pattern library customization.
1413
15-
This tag reads the CUSTOM_CSS setting from PATTERN_LIBRARY and includes
16-
the CSS file as a <link> tag. The CSS file should contain CSS custom properties
17-
that override the default pattern library styles.
14+
This tag optionally includes an external CSS file for additional customization.
1815
1916
Usage in templates:
2017
{% load pattern_library_tags %}
@@ -24,23 +21,28 @@ def pattern_library_custom_css():
2421
PATTERN_LIBRARY = {
2522
"CUSTOM_CSS": "css/pattern-library-custom.css" # relative to STATIC_URL
2623
}
27-
28-
Example custom CSS file content:
29-
:root {
30-
--color-primary: #ff6b6b;
31-
--family-primary: 'Custom Font', sans-serif;
32-
--site-title: 'My Custom Library';
33-
}
3424
"""
25+
css_content = []
26+
27+
# Include external CSS file if specified
3528
custom_css_path = get_setting('CUSTOM_CSS')
29+
if custom_css_path:
30+
try:
31+
css_url = static(custom_css_path)
32+
css_content.append(f'<link rel="stylesheet" type="text/css" href="{css_url}">')
33+
except Exception:
34+
pass # If static file handling fails, just skip the external file
35+
36+
return mark_safe('\n'.join(css_content))
37+
3638

37-
if not custom_css_path:
38-
return ''
39+
@register.simple_tag
40+
def pattern_library_site_title():
41+
"""
42+
Get the site title for the pattern library.
3943
40-
# Generate static URL for the CSS file
41-
try:
42-
css_url = static(custom_css_path)
43-
return mark_safe(f'<link rel="stylesheet" type="text/css" href="{css_url}">')
44-
except Exception:
45-
# If static file handling fails, return empty string
46-
return ''
44+
Usage in templates:
45+
{% load pattern_library_tags %}
46+
{% pattern_library_site_title %}
47+
"""
48+
return get_setting('SITE_TITLE')

0 commit comments

Comments
 (0)