Skip to content

Commit

Permalink
Add setting to customize return if icon can't be loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Wiegand committed Jun 12, 2021
1 parent d056457 commit f215640
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ BS_ICONS_CACHE = os.path.join(STATIC_ROOT, 'icon_cache')
This will ensure that icons will be rendered only once with their individual svg properties
and stored to a local file. On each subsequent use the icon will be simply loaded from file.

### Configure icon not found return

In case icons are not found you can configure, what to display:

```
BS_ICONS_NOT_FOUND = f"Icon <{icon_path}> does not exist"
```

This shows the error message if you for example misspelled an icon name.

If you're running your app offline you may want to display some value that has the same size as the icon:

```
BS_ICONS_NOT_FOUND = '<?xml version="1.0" ?>\
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="red" class="bi bi-x-circle" viewBox="0 0 16 16">\
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/>\
<path d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z"/>\
</svg>'
```

## Example App

There's an [Example App](https://github.com/christianwgd/django-bootstrap-icons-sample)
Expand Down Expand Up @@ -206,6 +226,7 @@ This project is licensed under the MIT License - see the
* django-bootstrap-icons 0.6.0 (Mai 2021): Update default bootstrap icons CDN to version 1.5.0
* django-bootstrap-icons 0.6.1 (June 2021): Fix path building on windows (#3)
* django-bootstrap-icons 0.6.2 (June 2021): Add icon cache to avoid multiple redering of same icon (#5)
* django-bootstrap-icons 0.6.3 (June 2021): Add a configuration option what to display if icons are not found.

## Migration from 0.2

Expand Down
29 changes: 20 additions & 9 deletions django_bootstrap_icons/templatetags/bootstrap_icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,26 @@ def icon(icon_path, icon_name, size=None, color=None, extra_classes=None):
return open(cache_file, 'r').read()

# cached icon doesn't exist or no cache configured, create and return icon
resp = requests.get(icon_path)
if resp.status_code >= 400:
return f"Icon <{icon_path}> does not exist"

content = xml.dom.minidom.parseString(resp.text)
svg = render_svg(content, size, color, extra_classes)
# if cache configured write icon to cache
if cache_path and cache_file:
open(cache_file, 'w').write(svg)
try:
resp = requests.get(icon_path)
if resp.status_code >= 400:
# return f"Icon <{icon_path}> does not exist"
return getattr(
settings,
'BS_ICONS_NOT_FOUND',
f"Icon <{icon_path}> does not exist"
)
content = xml.dom.minidom.parseString(resp.text)
svg = render_svg(content, size, color, extra_classes)
# if cache configured write icon to cache
if cache_path and cache_file:
open(cache_file, 'w').write(svg)
except requests.ConnectionError:
return getattr(
settings,
'BS_ICONS_NOT_FOUND',
f"Icon <{icon_path}> does not exist"
)
return svg


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='django-bootstrap-icons',
version='0.6.2',
version='0.6.3',
packages=setuptools.find_packages(),
include_package_data=True,
description='A quick way to add Bootstrap Icons with Django template tags.',
Expand Down

0 comments on commit f215640

Please sign in to comment.