Giter Club home page Giter Club logo

django-cotton's Introduction

Cotton

Bringing component-based design to Django templates.

Document site

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.cotton_loader.Loader',
                # 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 />

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>

django-cotton's People

Contributors

wrabit avatar actions-user avatar shakedown-street avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.