Skip to main content

Slippers is a lightweight library for Django that makes your HTML components available as template tags.

Project description

Slippers

Slippers is a lightweight library for Django that makes your HTML components available as template tags.

{% card variant="small" %}
  <h1>Slippers is cool</h1>

  {% button %}Super cool{% endbutton %}
  {% button variant="secondary" %}Lit af{% endbutton %}
{% endcard %}

Why?

I want to be able to make reusable components, but the syntax for {% include %} is too verbose. Plus it doesn't allow me to specify child elements.

Show me how it works

First create your template. Wherever you would normally put it is fine.

{# myapp/templates/myapp/card.html #}
<div class="card">
  <h1 class="card__header">{{ heading }}</h1>
  <div class="card__body">
    {# Child elements are rendered by `{{ children }}` #}
    {{ children }}
  </div>
</div>

Next, create a components.yml file. By default, Slippers looks for this file in the root template folder.

# myapp/templates/components.yml
# Components that have child elements
block_components:
  card: "myapp/card.html"
 
# Components that don't have child elements
inline_components: 
  avatar: "myapp/avatar.html"

You can now use the components like so:

{% load slippers %}

{% card heading="Slippers is awesome" %}
  <span>Hello {{ request.user.full_name }}!</span>
{% endcard %}

And the output:

<div class="card">
  <h1 class="card__header">Slippers is awesome</h1>
  <div class="card__body">
    <span>Hello Ryland Grace!</span>
  </div>
</div>

Installation

pip install slippers

Add it to your INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'slippers',
    ...
]

Documentation

The components.yml file

This file should be placed at the root template directory. E.g. myapp/templates/components.yml.

The structure of the file is as follows:

# Components that have child elements are called "block" components
block_components:
  # The key determines the name of the template tag. So `card` would generate
  # `{% card %}{% endcard %}`
  # The value is the path to the template file as it would be if used with {% include %}
  card: "myapp/card.html"
 
# Components that don't have child elements are called "inline" components
inline_components: 
  avatar: "myapp/avatar.html"

This file also doubles as an index of available components which is handy.

Context

Unlike {% include %}, using the component template tag will not pass the current context to the child component. This is a design decision. If you need something from the parent context, you have to explicitly pass it in via keyword arguments, or use {% include %} instead.

{% with not_passed_in="Lorem ipsum" %}
  {% button is_passed_in="Dolor amet" %}Hello{% endbutton %}
{% endwith %}

License

MIT

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

slippers-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

slippers-0.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file slippers-0.1.0.tar.gz.

File metadata

  • Download URL: slippers-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.0 CPython/3.8.6 Darwin/20.5.0

File hashes

Hashes for slippers-0.1.0.tar.gz
Algorithm Hash digest
SHA256 37ec6b30c714496ff71127b0f6282e4fc2998a6af8adf54d5ac2905fae2c4756
MD5 c8e12a867aa8c7901c60c5a6608bd1d3
BLAKE2b-256 d192151b3106c50c4ee5dba92f70421a9b582952e01c54bbae1cfdc6df45eb31

See more details on using hashes here.

File details

Details for the file slippers-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: slippers-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.0 CPython/3.8.6 Darwin/20.5.0

File hashes

Hashes for slippers-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b18c48309a53e2b7e3ae722cc74e331d2899f087f95fa553af27e6e0fa762df6
MD5 a4dc4d8e5884c258b563326e4f8e7bfa
BLAKE2b-256 9faf42438da4f6b5886c1ddb98eea0d96b8977ff8ed4aedf375b6df1c435ed80

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