-
Notifications
You must be signed in to change notification settings - Fork 48
Add support for Cotton Directives (c-if, c-elif, c-else, c-for) #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Hi @IanLuan interesting use case of the pre compiler! Very vue-like so Im not sure everyone will appreciate it but it could be complimentary. When I have time I'll take a closer look and try it out. |
|
I’m glad you liked it @wrabit! Personally, the Vue-like style is one of my favorite things about Django Cotton, so I thought it’d be a good idea to explore it. I’m happy to answer any questions if you have them! |
|
I really like this! Would be great to have even cleaner templates. Hopefully you'll get some time soon to review this @wrabit |
|
While browsing through tools in this area I note similarity in this and the |
Hi @jasalt, thanks for sharing it, I didn’t know about that package! I still think Cotton Directives could be a better fit than using dj-angles for a few reasons:
But it’s definitely a nice package, thanks again for sharing it! |
|
@IanLuan I see now there has been reasonable support for the feature which is great and providing we have some caveat around it being entirely optional I think we can merge. Docs would require updating, new page and left menu “Flow Control Directives” would suffice. Due to lack of time in this moment I would welcome a PR, if not I will get round to it eventually just not sure when (new arrival in family). |
|
I'm considering django-cotton for a new project and find this to be a really elegant syntax. I usually reach for Vue so that definitely is part of the appeal for me. Cool PR! |
|
@radusuciu I’m also interested how this could play with clientside libraries with Vue-like features like Datastar or AlpineJS, former having these as builtins like https://alpinejs.dev/directives/for and Datastar supporting it as custom attribute plugins. |
Congrats, man! I just had a new arrival as well, I understand you, haha. I'll try to create the documentation this weekend and push it to this PR so you can review it. Congrats again, enjoy the moment! |
About this proposal
This is a feature proposal introducing Cotton Directives — an HTML-based syntax for control flow in templates (
c-if,c-elif,c-else,c-for). Let me know if you'd prefer this to be discussed in a separate thread first.Thanks for this great library — it's been a lot of fun to work with!
Description
This PR introduces Cotton Directives, a better way to write control flow statements (
if,elif,else,for) following django_cotton’s philosophy. These directives use HTML attributes such asc-ifandc-for, which can be applied to any HTML element, not limited to Django Cotton components.Currently, writing control flow in Django templates requires verbose template tags like
{% if %},{% endif %}, which can disrupt the HTML structure and developer experience.This feature adds a cleaner, attribute-based syntax that aligns better with the design of django_cotton, enabling more readable and maintainable templates.
How It Works
Cotton Directives are implemented as special HTML attributes (
c-if,c-elif,c-else,c-for) that are parsed and translated into the equivalent Django template tag during the loader phase.When the template is processed:
{% %}Django template tag.Example
Will be rendered as:
{% if user.is_authenticated %} <div> <h1>Welcome, {{ user.username }}</h1> </div> {% endif %}Example
Which renders to:
Under the hood, the feature extends Python’s built-in
HTMLParserto parse the template HTML string.The parser detects elements containing the available directives, manages a stack-based structure to properly handle nested directives, and constructs the corresponding Django template tags around the matched HTML blocks.
Extensibility
This initial implementation adds Cotton Directives specifically for control flow statements such as conditionals (
c-if,c-elif,c-else) and loops (c-for). However, the design is intentionally extensible.In the future, additional Cotton Directives can be introduced easily, since any Django template block tag could potentially be represented as a Cotton Directive.
Performance
The Cotton Directives parser is blazing fast, thanks to Python’s built-in HTMLParser, which powers the feature.
In a test where 100 block tags were replaced with their respective directives (50 c-if and 50 c-for), the parser processed all 100 directives and transformed them into the corresponding Django block tags, adding only ~4ms to the final render time (measured on a notebook with an Intel i5-10210U processor).