


django-components v Templating is now on par with Vue or React
Hey, I'm Juro, I'm one of the maintainers of django-components. In releases v0.90-0.94 we've added features that make using components in templates much more flexible, similar to JSX / Vue.
(This info is already a bit dated (released a month ago; latest is v0.101), as I'm busy adding support for JS / CSS variables, TypeScript & Sass, and HTML fragment. Exciting stuff! But I realized haven't shared this update yet!)
Anyway, The following is a component blog_post, that accepts a title, id, and additional kwargs applied from blog_post_props:
1 2 3 4 5 |
|
The above is a combination of multiple features:
1. Self-closing tags:
Instead of
1 2 |
|
You can now simply write
1 |
|
2. Multi-line tags:
django_components now automatically configures Django to allow multi-line tags. So instead of cramming everything on a single line:
1 2 |
|
You can spread it across multiple lines:
1 2 3 4 5 |
|
3. Spread operator:
Similarly to ...props operator in JSX or v-bind in Vue, this inserts props / kwargs into a given position.
So instead of
1 2 3 4 5 |
|
You can have the kwargs in a dictionary, and then apply that:
1 2 3 4 5 6 |
|
1 2 |
|
4. Template tags inside string literals in component inputs:
You can now use template tags and flters inside component inputs:
1 2 3 4 5 6 |
|
This way you don't have to define extra variables every time you need to format a value.
Note that when there is only a single tag and no extra text around it, then the result is passed as a value. So "{% random_int 10 20 %}" passes in a number, and "{{ editable|not }}" passes a boolean.
You can even go a step further and have a similar experience to Vue or React, where you can evaluate arbitrary code expressions, AKA similar to this:
1 2 3 |
|
This can be possible with django-expr, which adds an expr tag and filter that you can use to evaluate Python expressions from within the template:
1 2 3 |
|
5. Support for {% comp_name %} {% endcomp_name %} and TagFormatter
By default, the components are written using the component tag, followed by the name of the component:
1 2 3 |
|
You can now change this (and even make your own!).
For example, setting COMPONENTS.tag_formatter to "django_components.shorthand_component_formatter" allows you to write components like so:
1 2 3 |
|
Lots more is to come, so be sure to give django-components a try!
The above is the detailed content of django-components v Templating is now on par with Vue or React. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Fastapi ...

Using python in Linux terminal...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

About Pythonasyncio...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Discussion on the reasons why pipeline files cannot be written when using Scapy crawlers When learning and using Scapy crawlers for persistent data storage, you may encounter pipeline files...
