Custom Django template tags for integrating Hyperscript with Django templates.
Project description
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
- Install using pip:
pip install django-hyperscript
- Add
django_hyperscripttoINSTALLED_APPSin your Django project'ssettings.py:
# settings.py
INSTALLED_APPS = [
...,
'django_hyperscript',
]
- Load the tag library in the necessary templates:
{% load hyperscript %}
Usage
By default, django-hyperscript wraps its output in a <div> with a class of hs-wrapper.
hs_dump
Dumps data into a single Hyperscript variable.
{% hs_dump data 'myData' %}
assuming data is {"foo": "bar"}, the tag would output
<div class="hs-wrapper" _="
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 class="hs-wrapper" _="
init
set global foo to bar
set global baz to qux
then remove me
end"></div>
Model Serialization
Django-Hyperscript includes a built-in lightweight serializer to help convert Django Model instances and QuerySets into plain data structures for use in templates. This includes instances or querysets that are nested within dictionaries or lists, which will be serialized recursively.
By default, the serializer returns all editable fields defined on the model. However, you can customize this by adding an hs_fields attribute to your model, which is a list of field names you want exposed.
class Book(models.Model):
name = models.CharField(max_length=32)
year_published = models.SmallIntegerField()
hs_fields = ['name', 'year_published']
would return an instance as
{
"name": "foo",
"year_published": 1986
}
This serializer is intentionally minimal and does not support advanced features like relation traversal, nested serialization, field aliasing, or custom computed fields. If you need those capabilities, consider serializing your data ahead of time using Django REST Framework, or a custom serializer.
If needed, this serializer can be accessed via
from django_hyperscript.serializer import hs_serialize
Configuration
Both hs_dump and hs_expand have a set of additional keyword arguments to configure their behavior.
show
Type: bool | Default: False
Keeps the element the Hyperscript is on in 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).
event
Type: str | Default: init
Specifies the event that triggers assignment. The Hyperscript "on" keyword should not need be provided.
Note: If show is False (which it is by default), the element will not be removed until after the event is fired and values are set.
wrap
Type: bool | Default: True
Wraps the Hyperscript in a <div> with its display set to none if True, otherwise returns the raw Hyperscript text.
Note: If both wrap and show are False, the element will not be removed and the only Hyperscript attribute and value will be removed from the element.
class
Type: str | Default: hs-wrapper
Sets the HTML class/classes on the wrapper <div>.
debug
Type: bool | Default: True
Logs the set variable name(s) and value(s).
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
showisTrue - The keys within the dumped data remain in snake case since
translateisFalse - The variable is scoped to the element the Hyperscript belongs to since
scopeis set to'element' - The output is just the raw Hyperscript text since
wrapis set toFalse
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_hyperscript-1.4.0.tar.gz.
File metadata
- Download URL: django_hyperscript-1.4.0.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cfd6a02762e6f464d15fdbdf873c85a04aec5bb569a6675a685e2d4ad1e879ed
|
|
| MD5 |
cb042ee2f4da5a40e118fde1bb384f1c
|
|
| BLAKE2b-256 |
061b40850d9311d44d2eeb0e041953ec936b3f9c6cdb66a4038d0a7a46a6371a
|
File details
Details for the file django_hyperscript-1.4.0-py3-none-any.whl.
File metadata
- Download URL: django_hyperscript-1.4.0-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18f9368e1fb5d28022814d03b5202c028841890f2db361a97261bb3601f08213
|
|
| MD5 |
63b42ac74d3f5bb5e27472a2e2c295a6
|
|
| BLAKE2b-256 |
2a0c19e3ed215db1b1e25a48bc8f03c980b5c1fb47ee1ffc2db93b327ea642d9
|