{% icon %}
Concept
icon combines <svg> (via named *.html) with a <span> (before or after) it and an optional parent tag around it.
| Invocation via Django Template Language | |
|---|---|
name='x_mark_mini'refers to a heroicon (default) svg copy/pasted into a file named 'heroicon_x.html'- The
aria_hiddenattribute is converted toaria-hidden,pre_textandpre_classmeans add a<span class='sr-only'>Close menu<span>before (pre)_ the svg icon.
<html>
<span class="sr-only">Close menu</span>
<svg
aria-hidden="true" class="w-6 h-6" fill="none" stroke="currentColor" stroke-width="1.5" viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path d="M6 18L18 6M6 6l12 12" stroke-linecap="round" stroke-linejoin="round">
</path>
</svg>
</html>
Setup
In order to work, must configure settings.py (or its equivalent) to define a path and then add properly named .html files containing copy/pasted raw <svg> from sources.
FRAGMENTS = {
"icons_prefix": "heroicons", # prefix
"icons_path": BASE_DIR / "templates" / "svg" # folder must exist, must use Path
}
SVG Placement
-
Copy
<svg>markup from a source such as heroicons, bootstrap, etc.;The svg markup in the file will look like:
x_mark_mini<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5"> <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" /> </svg> -
The name of the file to be created is relevant. Note source, we'll call this the
prefix(seeFRAGMENTS['icons_prefix']); -
Create
.htmlfile found inFRAGMENTS['icons_path']in the proper folder and paste the<svg>markup to that file. See placement:Prefix of source, name of svg -
Dissected (prefix used)
bootstrap; (icon name)github. Use as:{% icon name='github' prefix="bootstrap" %} -
Dissected:
- prefix used:
heroicons - icon name:
x_mark_mini
Use as:
{% icon name='x_mark_mini' prefix="heroicons" %}; or{% icon name='x_mark_mini' %}(since heroicons is the default).
- prefix used:
/src/templates/svg/heroicons_x_mark_mini.html<!-- note the created file's name --> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5"> <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" /> </svg> -
-
Filename follows convention
prefix+_+name(of the<svg>from the source).html. - The user is responsible for renaming the file properly to match the
prefixandname. Note thenamemust replace dashes-with underscores_.
Basis
Make an <svg> fragment, using a file found within the name + prefix + folder path, and add the appropriate
css classes and attributes, optionally including parent/sibling tags when parameters dictate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str
|
The prefixless (prefix_) name of the |
required |
css |
str
|
Previously defined CSS to add to the |
None
|
prefix |
str
|
Source of the svg file; needs to be declared in |
settings.FRAGMENTS.get('icons_prefix')
|
folder |
str
|
Where to find the template; needs to be declared in |
settings.FRAGMENTS.get('icons_path')
|
**kwargs |
dict
|
The following kwargs: |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
SafeText |
SafeText
|
Small HTML fragment visually representing an svg icon but which may contain related tags. |