Bringing component based design to Django templates.
Project description
Cotton
Bringing component-based design to Django templates.
Overview
Cotton enhances Django templates by allowing component-based design, making UI composition more efficient and reusable. It integrates seamlessly with Tailwind CSS and retains full compatibility with native Django template features.
Key Features
- Rapid UI Composition: Efficiently compose and reuse UI components.
- Tailwind CSS Harmony: Integrates with Tailwind's utility-first approach.
- Interoperable with Django: Enhances Django templates without replacing them.
- Semantic Syntax: HTML-like syntax for better code editor support.
- Minimal Overhead: Compiles to native Django components with automatic caching.
Getting Started
Installation
To install Cotton, run the following command:
pip install django-cotton
Then update your settings.py
:
INSTALLED_APPS = [
...
'django_cotton',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['your_project/templates'],
'APP_DIRS': False,
'OPTIONS': {
'loaders': [
'django_cotton.template.loaders.CottonLoader',
# continue with default loaders:
# "django.template.loaders.filesystem.Loader",
# "django.template.loaders.app_directories.Loader",
],
'builtins': [
'django_cotton.templatetags.cotton',
],
},
},
]
Quickstart
Create a new directory in your templates directory called cotton
. Inside this directory, create a new file called card.cotton.html
with the following content:
<div class="bg-white shadow rounded border p-4">
<h2>{{ title }}</h2>
<p>{{ slot }}</p>
<button href="{% url url %}">Read more</button>
</div>
Create a view with a template. Views that contain Cotton components must also use the .cotton.html
extension:
# views.py
def dashboard_view(request):
return render(request, "dashboard.cotton.html")
<!-- templates/dashboard.cotton.html -->
<c-card title="Trees" url="trees">
We have the best trees
</c-card>
<c-card title="Spades" url="spades">
The best spades in the land
</c-card>
Usage Basics
- Template Extensions: View templates including Cotton components should use the
.cotton.html
extension. - Component Placement: Components should be placed in the
templates/cotton
folder. - Naming Conventions:
- Component filenames use snake_case:
my_component.cotton.html
- Components are called using kebab-case:
<c-my-component />
- Component filenames use snake_case:
Example
A minimal example using Cotton components:
<!-- my_component.cotton.html -->
{{ slot }}
<!-- my_view.cotton.html -->
<c-my-component>
<p>Some content</p>
</c-my-component>
Attributes and Slots
Components can accept attributes and named slots for flexible content and behavior customization:
<!-- weather.cotton.html -->
<p>It's {{ temperature }}<sup>{{ unit }}</sup> and the condition is {{ condition }}.</p>
<!-- view.cotton.html -->
<c-weather temperature="23" unit="c" condition="windy"></c-weather>
Passing Variables
To pass a variable from the parent's context, prepend the attribute with a :
.
<!-- view.cotton.html -->
<c-weather :unit="unit"></c-weather>
Named Slots
<!-- weather_card.cotton.html -->
<div class="flex ...">
<h2>{{ day }}:</h2> {{ icon }} {{ label }}
</div>
<!-- view.cotton.html -->
<c-weather-card day="Tuesday">
<c-slot name="icon">
<svg>...</svg>
</c-slot>
<c-slot name="label">
<h2 class="text-yellow-500">Sunny</h2>
</c-slot>
</c-weather-card>
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
Hashes for django_cotton-0.9.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8d7b3ac02fb7d760527ff24fd46b123ef90a256e7e2d3919b191aa44f77d91d |
|
MD5 | 3dd5bf38425ba1d8e5944676152171ae |
|
BLAKE2b-256 | aacdb0a2d9f87a4ea5753a2b6b2c5420d24971af303eb8059ac2015f8e4ff117 |