Skip to main content

Use heroicons in your Django and Jinja templates.

Project description

https://img.shields.io/github/workflow/status/adamchainz/heroicons/CI/main?style=for-the-badge https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge https://img.shields.io/pypi/v/heroicons.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

Use heroicons in your Django and Jinja templates.

Requirements

Python 3.7 to 3.10 supported.

Django 2.2 to 4.0 supported.


Working on a Django project? Improve your skills with one of my books.


Usage

The heroicons package supports both Django templates and Jinja templates. Follow the appropriate guide below.

Django templates

  1. Install with python -m pip install heroicons[django].

  2. Add to your INSTALLED_APPS:

    INSTALLED_APPS = [
        ...,
        "heroicons",
        ...,
    ]

Now in your templates you can load the template library with:

{% load heroicons %}

This provides two tags to render SVG icons: heroicon_outline and heroicon_solid, corresponding to the two icon styles in the set. The tags take these arguments:

  • name, positional: the name of the icon to use. You can see the icon names on the heroicons.com grid.

  • size, keyword: an integer that will be used for the width and height attributes of the output <svg> tag. Defaults to the icons’ designed sizes: 24 for outline and 20 for solid.

  • Any number of keyword arguments. These will be added as attributes in the output HTML. Underscores in attribute names will be replaced with dashes, allowing you to define e.g. data- attributes.

    Most attributes will be added to the <svg> tag containing the icon, but these attributes will be attached to the inner <path> tags instead:

    • stroke-linecap

    • stroke-linejoin

    • stroke-width

    • vector-effect

Examples

An outline “academic-cap” icon:

{% heroicon_outline "academic-cap" %}

The same icon, solid, at 40x40 pixels, with some CSS classes:

{% heroicon_outline "academic-cap" size=40 class="h-4 w-4 inline" %}

That icon again, but with the paths changed to a narrower stroke width, and a “data-controller” attribute declared:

{% heroicon_outline "academic-cap" stroke_width=1 data_controller="academia" %}

Jinja templates

  1. Install with python -m pip install heroicons[jinja].

  2. Adjust your Jinja Environment to add the two global functions heroicon_outline and heroicon_solid, imported from heroicons.jinja. For example:

    from heroicons.jinja import heroicon_outline, heroicon_solid
    from jinja2 import Environment
    
    env = Environment()
    env.globals.update(
        {
            "heroicon_outline": heroicon_outline,
            "heroicon_solid": heroicon_solid,
        }
    )

Now in your templates you can call those two functions, which render <svg> icons corresponding to the two icon styles in the set. The functions take these arguments:

  • name, positional: the name of the icon to use. You can see the icon names on the heroicons.com grid.

  • size, keyword: an integer that will be used for the width and height attributes of the output <svg> tag. Defaults to the icons’ designed sizes: 24 for outline and 20 for solid.

  • Any number of keyword arguments. These will be added as HTML attributes to the output HTML. Underscores in attribute names will be replaced with dashes, allowing you to define e.g. data- attributes.

    Most attributes will be added to the <svg> tag containing the icon, but these attributes will be attached to the inner <path> tags instead:

    • stroke-linecap

    • stroke-linejoin

    • stroke-width

    • vector-effect

Examples

An outline “academic-cap” icon:

{{ heroicon_outline("academic-cap") }}

The same icon, solid, at 40x40 pixels, with some CSS classes:

{{ heroicon_solid("academic-cap", size=40, class="h-4 w-4 inline") %}

That icon again, but with the paths changed to a narrower stroke width, and a “data-controller” attribute declared:

{{ heroicon_outline("academic-cap", stroke_width=1, data_controller="academia") %}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

heroicons-1.5.0.tar.gz (124.1 kB view details)

Uploaded Source

Built Distribution

heroicons-1.5.0-py3-none-any.whl (120.8 kB view details)

Uploaded Python 3

File details

Details for the file heroicons-1.5.0.tar.gz.

File metadata

  • Download URL: heroicons-1.5.0.tar.gz
  • Upload date:
  • Size: 124.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for heroicons-1.5.0.tar.gz
Algorithm Hash digest
SHA256 c81c2e8d844163e3bf8740a3c1451591801576fb73ed114ee20d7f9563576d91
MD5 c3247ba13ed7e94953d7afa5f918d67b
BLAKE2b-256 1a23f25e6c506d3bb3db59a56d0a1771f95df0167845c78ffe41d704d643497a

See more details on using hashes here.

File details

Details for the file heroicons-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: heroicons-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 120.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for heroicons-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 928af33cda0337946c4ac3c5abe697fca3fca15e16004e99edcafeed691cf252
MD5 be784a23d20b83e7cf871f11721539ca
BLAKE2b-256 b64d831d73a7730168132019d2bbce8d634e987e01a98005e3b9fb26a1d0b56c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page