Skip to main content

A set of tools to help simplify your Django templates.

Project description

README

A set of tools to help simplify your Django templates

$ pip install django-template-simplify

Then add the app into INSTALLED_APPS in settings.py

INSTALLED_APPS = [
    ...,
    'template_simplify',
]

dom_id

dom_id is a helper method that returns a unique DOM ID based on the object's class name and primary key.

{% load template_simplify %}

{% dom_id instance %}               ->  task_1
{% dom_id instance 'detail' %}      ->  detail_task_1
{% dom_id Task %}                   ->  new_task
  1. dom_id first argument can be string, instance or Model class
  2. dom_id second argument is optional string that will be used as prefix.

You can also use it in your Django view code.

from template_simplify import dom_id

target = dom_id(instance, "detail_container")

We can say goodbye to id="task-{{ task.pk }}" and use id="{% dom_id task %}" instead.

The benefit is, it simplified the DOM ID generation in Python and Django template, and avoid typo error in many cases.

class_names

Inspired by JS classnames and Rails class_names

class_names can help conditionally render css classes

{% load template_simplify %}

<div class="{% class_names test1=True 'test2' ring-slate-900/5=True already-sign-in=request.user.is_authenticated %}"></div>

'<div class="test1 test2 ring-slate-900/5 dark:bg-slate-800 %}"></div>'

It can work well with TailwindCSS's some special char such as / and :

Below is an example of if else

<div class="{% class_names active=request.user.is_authenticated inactive=!request.user.is_authenticated %}"></div>
  1. Please note ! can be used just like Javascript.
  2. So if user is not authenticated in this case, we would get <div class="inactive"></div>

This can help make css logic more organized.

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_template_simplify-1.0.3.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_template_simplify-1.0.3-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file django_template_simplify-1.0.3.tar.gz.

File metadata

File hashes

Hashes for django_template_simplify-1.0.3.tar.gz
Algorithm Hash digest
SHA256 acfe8f71d6695bc690edcfe95cb51166566a2f916f95f6ce106d1951575725c3
MD5 944cc758d4aa50e64fbf8410b1c1351f
BLAKE2b-256 c392cf6be933ee5e8017eb53db3d4f269e5c73b9fd94db70b3870fc8e59633db

See more details on using hashes here.

File details

Details for the file django_template_simplify-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_template_simplify-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4433d11ed26835ebfa88c76de9cb28a3fec52d727c092863fb711b56d221a526
MD5 f9eb7767608b956a6d950cb61c051e80
BLAKE2b-256 e01a7c0d1e08ae64c27781eab401bd419f87d1efb1b489f4a0a6ba83393bf18e

See more details on using hashes here.

Supported by

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