Skip to content

Commit 296875b

Browse files
committed
chore
1 parent bdb6990 commit 296875b

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,59 @@
11
# README
2+
3+
A set of tools to help simplify your Django templates
4+
5+
```shell
6+
$ pip install django-template-simplify
7+
```
8+
9+
Then add the app into `INSTALLED_APPS` in settings.py
10+
11+
```python
12+
INSTALLED_APPS = [
13+
...,
14+
'template_simplify',
15+
]
16+
```
17+
18+
## dom_id
19+
20+
`dom_id` is a helper method that returns a unique DOM ID based on the object's class name and primary key.
21+
22+
```html
23+
{% load template_simplify %}
24+
25+
{% dom_id instance %} -> task_1
26+
{% dom_id instance 'detail' %} -> detail_task_1
27+
{% dom_id Task %} -> new_task
28+
```
29+
30+
1. `dom_id` first argument can be string, instance or Model class
31+
2. `dom_id` second argument is optional string that will be used as `prefix`.
32+
33+
You can also use it in your Django view code.
34+
35+
```python
36+
from template_simplify import dom_id
37+
38+
target = dom_id(instance, "detail_container")
39+
```
40+
41+
We can say goodbye to `id="task-{{ task.pk }}"` and use `id="{% dom_id task %}"` instead.
42+
43+
The benefit is, **it simplified the DOM ID generation in Python and Django template, and avoid typo error in many cases.**
44+
45+
## class_names
46+
47+
Inspired by JS [classnames](https://www.npmjs.com/package/classnames) and Rails `class_names`
48+
49+
`class_names` can help **conditionally render css classes**
50+
51+
```html
52+
{% load template_simplify %}
53+
54+
<div class="{% class_names test1=True 'test2' ring-slate-900/5=True already-sign-in=request.user.is_authenticated %}"></div>
55+
56+
'<div class="test1 test2 ring-slate-900/5 dark:bg-slate-800 %}"></div>'
57+
```
58+
59+
It can also work well with TailwindCSS's some special char such as `/` and `:`

0 commit comments

Comments
 (0)