Skip to main content

Custom Django template tags for integrating Hyperscript with Django templates.

Project description

Icon

Django Hyperscript

This package is intended to simplify the process of dumping data from Django into Hyperscript by providing two template tags with options for customizing the output.


Installation

  1. Install using pip:
pip install django-hyperscript
  1. Add django_hyperscript to INSTALLED_APPS in your Django project's settings.py:
# settings.py

INSTALLED_APPS = [
    ...,
    'django_hyperscript',
]
  1. Load the tag library in the necessary templates:
{% load hyperscript %}

Usage

hs_dump

Dumps data into a single Hyperscript variable.

{% hs_dump data 'myData' %}

assuming data is {"foo": "bar"}, the tag would output

<div _="
init
    set global myData to {'foo': 'bar'} 
    then remove me 
end"></div>

hs_expand

Expands a dictionary into Hyperscript variables.

{% hs_expand data %}

assuming data is {"foo": "bar", "baz": "qux"}, the tag would output

<div _="
init 
    set global foo to bar 
    set global baz to qux 
    then remove me
end"></div>

Configuration

Both hs_dump and hs_expand have a set of additional keyword arguments to configure their behavior.

show

Type: bool | Default: False

Removes Hyperscript from the DOM after initializing if True.

translate

Type: bool | Default: True

"Translates" dictionary keys from snake case (snake_case) to camel case (camelCase) to fit JavaScript naming conventions.

scope

Type: str | Default: global

Determines the scope of the Hyperscript variable (global, element, or local).

wrap

Type: bool | Default: True

Wraps the Hyperscript in a <div> if True, otherwise returns the raw Hyperscript text.

Note: If both wrap and show are False, the element will not be removed and the Hyperscript attribute and value will be removed from the element.

Final example

{% hs_dump data 'my_data' show=True translate=False scope='element' wrap=False %}

assuming data is {"my_value": 25}, the tag would output

"init set element my_data to {'my_value': 25} end"

In this example:

  • The Hyperscript remains in the DOM since show is True
  • The keys within the dumped data remain in snake case since translate is False
  • The variable is scoped to the element the Hyperscript belongs to since scope is set to 'element'
  • The output is just the raw Hyperscript text since wrap is set to False

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

django_hyperscript-1.0.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

django_hyperscript-1.0.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file django_hyperscript-1.0.1.tar.gz.

File metadata

  • Download URL: django_hyperscript-1.0.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for django_hyperscript-1.0.1.tar.gz
Algorithm Hash digest
SHA256 c192505554976368c82a50526a30ba89303f5387415e4e4a1f1f63df5890c1a0
MD5 67fed1e5a6111769c5ea766ad32d7128
BLAKE2b-256 181584fecb08e1acb500798072126991557316dd980a895b5ee2a4d84007e657

See more details on using hashes here.

File details

Details for the file django_hyperscript-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_hyperscript-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d5753e0872188203eec2dddcc6512f97d0bcf4f2642e43b90146a392a7665d39
MD5 401a4223c83986f44e3087f46f765051
BLAKE2b-256 7e3d1376a0b1182d146ddc9716b00632b75bdb974d6be8647bc8867bf1c1cf4f

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