django-fragments Docs
In active development
Expect breaking changes.
Invoke via {% load fragments %}
but note conventions
- Checking for the required number of arguments, etc., has already been done by the time our function is called, so we don’t need to do that.
- The quotes around the argument (if any) have already been stripped away, so we receive a plain string.
- If the argument was a template variable, our function is passed the current value of the variable, not the variable itself.
Simple tag conventions
{% icon
name='x_mark_mini'
parent_title="{{variable}}"
%}
{# won't work, multiline tags are not supported in Django #}
{% icon name='x_mark_mini' parent_title="{{variable}}" %}
{# won't work, the variable needs to be passed directly #}
{% icon name='x_mark_mini' parent_title=variable %}
{# works #}
{% with variable='This is a title' %}
{% icon name='x_mark_mini' parent_title=variable %}
{% endwith %}
{# works #}
Shortcuts
fragment | note |
---|---|
{% themer %} |
overrideable theme switcher, affecting <html class=?> |
{% icon %} |
idiomatic <svg> combiner with neighboring / parent tags |
{% hput %} |
optional inline validated <input> , is widget-tweakable |
{% nava %} |
Uses format_html to output an <a> element fit for desktop/mobile navbar links |
{% curr %} |
Outputs string aria-current=page if url is current |
Open Graph
fragment | note |
---|---|
{% og_title %} |
Adds to <title> and related open graph tags |
{% og_desc %} |
Adds to <meta name=description> and related open graph tags |
{% og_img %} |
Adds image-related open graph tags |
Helpers
fragment | note |
---|---|
{% whitespaceless %} |
Remove "space between tags and text", outside {% spaceless %} scope. |
{% htmx_csrf %} |
Adds idiomatic hx-header=csrf-token-variable |
These are partial templates, originally meant for a Django boilerplate, refactored out as independent library.